-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniqueBinarySearchTreesII.java
More file actions
73 lines (68 loc) · 2.02 KB
/
UniqueBinarySearchTreesII.java
File metadata and controls
73 lines (68 loc) · 2.02 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
62
63
64
65
66
67
68
69
70
71
72
73
package medium;
import java.util.ArrayList;
import java.util.List;
import util.TreeNode;
/**
*
* ClassName: UniqueBinarySearchTreesII
* @author chenyiAlone
* Create Time: 2019/04/11 17:52:50
* Description: No.95
* 思路:
* 1. l r 作为边界
* 2. 总的结点数为 n, 那么 i 为根的 Binary Search 就是以 [1 ... i - 1]为左子树
* 以及以 [i + 1 ... r] 的所有子树的组合
* 3. 边界条件
* a. l > r => return [[null]]
* b. l == r => return [[l]]
*
* Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ... n.
Example:
Input: 3
Output:
[
[1,null,3,2],
[3,2,null,1],
[3,1,null,null,2],
[2,1,3],
[1,null,2,null,3]
]
Explanation:
The above output corresponds to the 5 unique BST's shown below:
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
*/
public class UniqueBinarySearchTreesII {
public List<TreeNode> generateTrees(int n) {
if (n == 0)
return new ArrayList<TreeNode>();
return dfs(1, n);
}
List<TreeNode> dfs(int l, int r) {
List<TreeNode> res = new ArrayList<>();
if (l == r) {
res.add(new TreeNode(l));
return res;
}
if (l > r) {
res.add(null);
return res;
}
for (int i = l; i <= r; i++) {
List<TreeNode> lts = dfs(l, i - 1);
List<TreeNode> rts = dfs(i + 1, r);
for (TreeNode lt : lts) {
for (TreeNode rt : rts) {
TreeNode tmp = new TreeNode(i);
tmp.left = lt;
tmp.right = rt;
res.add(tmp);
}
}
}
return res;
}
}