-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniqueBinarySearchTreesII.java
More file actions
61 lines (52 loc) · 1.56 KB
/
UniqueBinarySearchTreesII.java
File metadata and controls
61 lines (52 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
/**
* reference to https://leetcode.com/discuss/10254/a-simple-recursive-solution
* I start by noting that 1..n is the in-order traversal for any BST with nodes 1 to n.
* So if I pick i-th node as my root, the left subtree will contain elements 1 to (i-1),
* and the right subtree will contain elements (i+1) to n.
* I use recursive calls to get back all possible trees for left and right subtrees
* and combine them in all possible ways with the root.
*/
public class Solution {
public List<TreeNode> generateTrees(int n) {
return genTrees(1,n);
}
public List<TreeNode> genTrees (int start, int end)
{
List<TreeNode> list = new ArrayList<TreeNode>();
if(start>end)
{
list.add(null);
return list;
}
if(start == end){
list.add(new TreeNode(start));
return list;
}
List<TreeNode> left,right;
for(int i=start;i<=end;i++)
{
left = genTrees(start, i-1);
right = genTrees(i+1,end);
for(TreeNode lnode: left)
{
for(TreeNode rnode: right)
{
TreeNode root = new TreeNode(i);
root.left = lnode;
root.right = rnode;
list.add(root);
}
}
}
return list;
}
}