class Solution {
public:
vector<TreeNode *> generateTrees(int n) {
return create(1,n);
}
vector<TreeNode*>create(int L,int R)
{
vector<TreeNode*>res;
if(L>R)
{
res.push_back(NULL);
return res;
}
for(int i=L;i<=R;i++)
{
vector<TreeNode*>Left=create(L,i-1);
vector<TreeNode*>Right=create(i+1,R);
for(auto j:Left)
{
for(auto k:Right)
{
TreeNode*root=new TreeNode(i);
root->left=j,root->right=k;
res.push_back(root);
}
}
}
return res;
}
};
网友评论