class Solution {
public:
int numTrees(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (n == 0) return 1;
int sum = 0;
for (int i = 0; i < n; ++i)
{
sum += numTrees(i) * numTrees(n - i - 1);
}
return sum;
}
};
class Solution {
public:
int numTrees(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (n <= 1) return n;
vector<int> ways(n + 1);
ways[0] = 1;
ways[1] = 1;
for (int i = 2; i <= n; ++i)
{
ways[i] = 0;
for (int j = 0; j < i; ++j)
{
int l = ways[j];
int r = ways[i - j - 1];
ways[i] += l * r;
}
}
return ways[n];
}
};