class Solution {
public:
int numTrees(int n) {
int* dp = new int[n+];
dp[] = ;
dp[] = ;
dp[] = ;
for(int i=;i<=n;i++){
dp[i] = ;
for(int j=;j<=i;j++){
dp[i]+=dp[j-]*dp[i-j];
}
}
return dp[n];
}
int numTrees2(int n){
if(n==) return ;
if(n < ) return n;
int sum = ;
for(int i=;i<=n;i++){
sum += numTrees2(i-)*numTrees2(n-i);
}
return sum;
}
};
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<TreeNode *> generateTrees(int left, int right) {
vector<TreeNode*> res;
if (left > right) {
res.push_back(nullptr);
return res;
}
if(left == right){
res.push_back(new TreeNode(left));
return res;
}
for (int i = left; i <= right; i++) {
//TreeNode* root = new TreeNode(i);
vector<TreeNode*> leftRes = generateTrees(left, i-);
vector<TreeNode*> rightRes = generateTrees(i + , right);
for (size_t j = ; j < leftRes.size(); j++) {
for (size_t k = ; k < rightRes.size(); k++) {
TreeNode* root = new TreeNode(i);
root->left = leftRes[j];
root->right = rightRes[k];
res.push_back(root);
}
}
}
return res;
}
vector<TreeNode*> generateTrees(int n){
vector<TreeNode*> res;
if(n == ) {
res.push_back(nullptr);
return res;
}
return generateTrees(,n);
}
};