Unique Binary Search Trees II
Source
Given n, generate all structurally unique BST's (binary search trees)
that store values 1...n.
Example
Given n = 3, your program should return all 5 unique BST's shown below.
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
Java
public class Solution {
public List<TreeNode> generateTrees(int n) {
if(n==0) return new ArrayList<TreeNode>();
return generateTrees(1, n);
}
public List<TreeNode> generateTrees(int start, int end) {
List<TreeNode> list = new LinkedList<>();
if (start > end) {
list.add(null);
return list;
}
for (int i = start; i <= end; i++) {
List<TreeNode> lefts = generateTrees(start, i - 1);
List<TreeNode> rights = generateTrees(i + 1, end);
for (TreeNode left : lefts) {
for (TreeNode right : rights) {
TreeNode node = new TreeNode(i);
node.left = left;
node.right = right;
list.add(node);
}
}
}
return list;
}
}