95. Unique Binary Search Trees II

Posted 鸵鸟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了95. Unique Binary Search Trees II相关的知识,希望对你有一定的参考价值。

class Solution {
    public List<TreeNode> generateTrees(int n) {
        if(n==0)
            return new ArrayList<TreeNode>();
        return generateTrees(1, n);
    }
    private List<TreeNode> generateTrees(int i, int j) {
        List<TreeNode> res=new ArrayList<TreeNode>();
        if(i>j)
            res.add(null);
        else
        {
            for(int k=i;k<=j;k++)
            {
                List<TreeNode> left=generateTrees(i,k-1);
                List<TreeNode> right=generateTrees(k+1,j);
                for(TreeNode l:left)
                    for(TreeNode r:right)
                    {
                        TreeNode node=new TreeNode(k);
                        node.left=l;
                        node.right=r;
                        res.add(node);
                    }
            }
        }
        return res;
    }
}

 

以上是关于95. Unique Binary Search Trees II的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 95. Unique Binary Search Trees II

Leetcode 95. Unique Binary Search Trees II

LeetCode 95. Unique Binary Search Trees II

95. Unique Binary Search Trees II

95. Unique Binary Search Trees II

[leetcode-95-Unique Binary Search Trees II]