Unique Binary Search Trees II
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1...n.
For 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
Solution
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<TreeNode> generateTrees(int n) {
return generate(1, n);
}
public List<TreeNode> generate(int start, int end) {
List<TreeNode> result = new ArrayList<>();
if (start > end) {
return result;
}
if (start == end) {
TreeNode node = new TreeNode(start);
result.add(node);
return result;
}
for (int i = start; i <= end; i++) {
List<TreeNode> left = generate(start, i - 1);
List<TreeNode> right = generate(i + 1, end);
if (left.isEmpty() && right.isEmpty()) {
TreeNode root = new TreeNode(i);
result.add(root);
} else if (left.isEmpty()) {
for (TreeNode r : right) {
TreeNode root = new TreeNode(i);
root.right = r;
result.add(root);
}
} else if (right.isEmpty()) {
for (TreeNode l : left) {
TreeNode root = new TreeNode(i);
root.left = l;
result.add(root);
}
} else {
for (TreeNode l : left) {
for (TreeNode r : right) {
TreeNode root = new TreeNode(i);
root.left = l;
root.right = r;
result.add(root);
}
}
}
}
return result;
}
}