-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem80.cpp
More file actions
46 lines (39 loc) · 816 Bytes
/
problem80.cpp
File metadata and controls
46 lines (39 loc) · 816 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <bits/stdc++.h>
using namespace std;
class GardenOfBST {
int n;
vector<int> dp;
public:
void readInput() {
cin >> n;
if (n < 1 || n > 19) {
cout << "!! Invalid Input !!" << endl;
exit(1);
}
dp.assign(n + 1, -1);
}
int countBST(int nodes) {
// Base case
if (nodes <= 1) return 1;
// Already computed
if (dp[nodes] != -1) return dp[nodes];
int total = 0;
// Choose each node as root
for (int root = 1; root <= nodes; ++root) {
int left = countBST(root - 1);
int right = countBST(nodes - root);
total += left * right;
}
dp[nodes] = total;
return total;
}
void display() {
cout << countBST(n) << endl;
}
};
int main() {
GardenOfBST garden;
garden.readInput();
garden.display();
return 0;
}