@@ -0,0 +1,999 @@
+package problems;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0Apublic class UniqueBinarySearchTrees2 %7B%0A%0A    public class TreeNode %7B%0A        int val;%0A        TreeNode left, right;%0A%0A        TreeNode(int x) %7B%0A            val = x;%0A        %7D%0A    %7D%0A%0A    public List%3CTreeNode%3E generateTrees(int n) %7B%0A        return trees(1, n);%0A    %7D%0A    %0A    List%3CTreeNode%3E trees(int l, int r) %7B%0A        List%3CTreeNode%3E out = new ArrayList%3CTreeNode%3E();%0A        if (r %3C l) %7B%0A            out.add(null);%0A            return out;%0A        %7D%0A        %0A        for (int i = l; i %3C= r; i++) %7B %0A            List%3CTreeNode%3E left = trees(l, i - 1);%0A            List%3CTreeNode%3E right = trees(i + 1, r);%0A            %0A            for (TreeNode t1 : left) %7B%0A                for (TreeNode t2 : right) %7B%0A                    TreeNode node = new TreeNode(i);%0A                    node.left = t1; %0A                    node.right = t2;%0A                    out.add(node);%0A                %7D%0A            %7D%0A        %7D%0A        return out;%0A    %7D%0A%7D%0A

Add Unique Binary Search Trees 2