Leetcode Unique Binary Search Trees II

public class Solution {
    public ArrayList<TreeNode> generateTrees(int n) {
        return generateTrees(1, n);
    }

    public ArrayList<TreeNode> generateTrees(int start, int end){
        ArrayList<TreeNode> unique = new ArrayList<TreeNode>();
        if(start > end){
            unique.add(null);
            return unique;
        }
        for(int i = start; i <= end; i++){
            for(TreeNode left: generateTrees(start, i - 1)){
                for(TreeNode right: generateTrees(i + 1, end)){
                    TreeNode root = new TreeNode(i);
                    root.left = left;
                    root.right = right;
                    unique.add(root);
                }
            }
        }
        return unique;
    }       
}

 

posted @ 2013-09-05 19:03  代码改变未来  阅读(1316)  评论(0编辑  收藏  举报