-
Notifications
You must be signed in to change notification settings - Fork 0
/
095_UniqueBinarySearchTreesII95.java
168 lines (147 loc) · 5.04 KB
/
095_UniqueBinarySearchTreesII95.java
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* 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
*
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class UniqueBinarySearchTreesII95 {
public List<TreeNode> generateTrees(int n) {
return generateTrees(n, 1, n);
}
public List<TreeNode> generateTrees(int n, int l, int r) {
List<TreeNode> res = new ArrayList<>();
if (l > r) return res;
for (int i=l; i<=r; i++) {
List<TreeNode> lefts = generateTrees(n, l, i-1);
if (lefts.isEmpty()) lefts.add(null);
List<TreeNode> rights = generateTrees(n, i+1, r);
if (rights.isEmpty()) rights.add(null);
for (TreeNode ll: lefts) {
for (TreeNode rr: rights) {
TreeNode t = new TreeNode(i);
t.left = ll;
t.right = rr;
res.add(t);
}
}
}
return res;
}
// DP
public List<TreeNode> generateTrees2(int n) {
return generateTrees(n, 1, n, new List[n][n]);
}
public List<TreeNode> generateTrees(int n, int l, int r, List[][] dp) {
List<TreeNode> res = new ArrayList<>();
if (l > r) return res;
if (dp[l-1][r-1] != null) return dp[l-1][r-1];
for (int i=l; i<=r; i++) {
List<TreeNode> lefts = generateTrees(n, l, i-1, dp);
if (lefts.isEmpty()) lefts.add(null);
List<TreeNode> rights = generateTrees(n, i+1, r, dp);
if (rights.isEmpty()) rights.add(null);
for (TreeNode ll: lefts) {
for (TreeNode rr: rights) {
TreeNode t = new TreeNode(i);
t.left = ll;
t.right = rr;
res.add(t);
}
}
}
dp[l-1][r-1] = res;
return res;
}
public List<TreeNode> generateTrees3(int n) {
if (n == 0) return new ArrayList<TreeNode>();
List<TreeNode>[][] dp = new List[n][n];
List<TreeNode> lefts;
List<TreeNode> rights;
for (int len=1; len<=n; len++) {
for (int start=1; start<=n+1-len; start++) {
int end = start + len - 1;
List<TreeNode> list = new ArrayList<>();
for (int k=start; k<=end; k++) {
if (start > k-1) {
lefts = new ArrayList<TreeNode>();
lefts.add(null);
} else {
lefts = dp[start-1][k-2];
}
if (k+1 > end) {
rights = new ArrayList<TreeNode>();
rights.add(null);
} else {
rights = dp[k][end-1];
}
for (TreeNode left: lefts) {
for (TreeNode right: rights) {
TreeNode curr = new TreeNode(k);
curr.left = left;
curr.right = right;
list.add(curr);
}
}
}
dp[start-1][end-1] = list;
}
}
return dp[0][n-1];
}
public List<TreeNode> generateTrees4(int n) {
if (n == 0) return new ArrayList<TreeNode>();
List<TreeNode>[][] dp = new List[n][n];
for (int i=0; i<n; i++) {
List<TreeNode> l = new ArrayList<>();
l.add(new TreeNode(i+1));
dp[i][i] = l;
}
for (int len=1; len<n; len++) {
for (int i=0; i<n-len; i++) {
List<TreeNode> l = new ArrayList<>();
for (int j=i; j<=i+len; j++) {
add(dp, i, j, i+len, l);
}
dp[i][i+len] = l;
}
}
return dp[0][n-1];
}
private void add(List<TreeNode>[][] dp, int i, int j, int k, List<TreeNode> l) {
List<TreeNode> lefts = get(dp, i, j-1);
List<TreeNode> rights = get(dp, j+1, k);
for (TreeNode lf: lefts) {
for (TreeNode rt: rights) {
TreeNode n = new TreeNode(j+1);
n.left = lf;
n.right = rt;
l.add(n);
}
}
}
private List<TreeNode> get(List<TreeNode>[][] dp, int i, int j) {
if (i > j) {
List<TreeNode> l = new ArrayList<>();
l.add(null);
return l;
}
return dp[i][j];
}
}