美文网首页
unique-binary-search-trees-ii

unique-binary-search-trees-ii

作者: DaiMorph | 来源:发表于2019-05-27 23:12 被阅读0次
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;
    }
};

网友评论

      本文标题:unique-binary-search-trees-ii

      本文链接:https://www.haomeiwen.com/subject/falltctx.html