-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathMinimumHeightTrees310.java
More file actions
265 lines (235 loc) · 7.48 KB
/
MinimumHeightTrees310.java
File metadata and controls
265 lines (235 loc) · 7.48 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
* For a undirected graph with tree characteristics, we can choose any node as
* the root. The result graph is then a rooted tree. Among all possible rooted
* trees, those with minimum height are called minimum height trees (MHTs).
* Given such a graph, write a function to find all the MHTs and return a list
* of their root labels.
*
* Format
* The graph contains n nodes which are labeled from 0 to n - 1. You will be
* given the number n and a list of undirected edges (each edge is a pair of
* labels).
*
* You can assume that no duplicate edges will appear in edges. Since all edges
* are undirected, [0, 1] is the same as [1, 0] and thus will not appear
* together in edges.
*
* Example 1:
*
* Given n = 4, edges = [[1, 0], [1, 2], [1, 3]]
*
* 0
* |
* 1
* / \
* 2 3
* return [1]
*
* Example 2:
*
* Given n = 6, edges = [[0, 3], [1, 3], [2, 3], [4, 3], [5, 4]]
*
* 0 1 2
* \ | /
* 3
* |
* 4
* |
* 5
* return [3, 4]
*
* Note:
*
* (1) According to the definition of tree on Wikipedia: “a tree is an
* undirected graph in which any two vertices are connected by exactly one path.
* In other words, any connected graph without simple cycles is a tree.”
*
* (2) The height of a rooted tree is the number of edges on the longest
* downward path between the root and a leaf.
*
*/
public class MinimumHeightTrees310 {
public List<Integer> findMinHeightTrees(int n, int[][] edges) {
Map<Integer, Map<Integer, Integer>> map = new HashMap<>();
for (int i=0; i<n; i++) {
map.put(i, new HashMap<>());
}
for (int[] e: edges) {
map.get(e[0]).put(e[1], -1);
map.get(e[1]).put(e[0], -1);
}
List<Integer> res = new ArrayList<>();
int minH = Integer.MAX_VALUE;
for (int p=0; p<n; p++) {
boolean[] marked = new boolean[n];
marked[p] = true;
int newHeight = search(p, 0, map, marked);
marked[p] = false;
if (newHeight < minH) {
res = new ArrayList<>();
res.add(p);
minH = newHeight;
} else if (newHeight == minH) {
res.add(p);
}
}
return res;
}
private int search(Integer p, Integer h, Map<Integer, Map<Integer, Integer>> map, boolean[] marked) {
Map<Integer, Integer> connects = map.get(p);
int max = 0;
for (Map.Entry<Integer, Integer> c: connects.entrySet()) {
if (marked[c.getKey()]) {
max = Math.max(max, h);
continue;
}
if (c.getValue() != -1) {
max = Math.max(max, c.getValue());
continue;
}
marked[c.getKey()] = true;
int cValue = search(c.getKey(), h, map, marked);
marked[c.getKey()] = false;
c.setValue(cValue);
max = Math.max(max, cValue);
}
return max+1;
}
/**
* https://discuss.leetcode.com/topic/30572/share-some-thoughts
*/
public List<Integer> findMinHeightTrees2(int n, int[][] edges) {
if (n == 1) return Collections.singletonList(0);
List<Set<Integer>> adj = new ArrayList<>(n);
for (int i = 0; i < n; ++i) adj.add(new HashSet<>());
for (int[] edge : edges) {
adj.get(edge[0]).add(edge[1]);
adj.get(edge[1]).add(edge[0]);
}
List<Integer> leaves = new ArrayList<>();
for (int i = 0; i < n; ++i)
if (adj.get(i).size() == 1) leaves.add(i);
while (n > 2) {
n -= leaves.size();
List<Integer> newLeaves = new ArrayList<>();
for (int i : leaves) {
int j = adj.get(i).iterator().next();
adj.get(j).remove(i);
if (adj.get(j).size() == 1) newLeaves.add(j);
}
leaves = newLeaves;
}
return leaves;
}
/**
* https://discuss.leetcode.com/topic/30956/two-o-n-solutions
*/
int n;
List<Integer>[] e;
private void bfs(int start, int[] dist, int[] pre) {
boolean[] visited = new boolean[n];
Queue<Integer> queue = new ArrayDeque<>();
queue.add(start);
dist[start] = 0;
visited[start] = true;
pre[start] = -1;
while (!queue.isEmpty()) {
int u = queue.poll();
for (int v : e[u])
if (!visited[v]) {
visited[v] = true;
dist[v] = dist[u] + 1;
queue.add(v);
pre[v] = u;
}
}
}
public List<Integer> findMinHeightTree3(int n, int[][] edges) {
if (n <= 0) return new ArrayList<>();
this.n = n;
e = new List[n];
for (int i = 0; i < n; i++)
e[i] = new ArrayList<>();
for (int[] pair : edges) {
int u = pair[0];
int v = pair[1];
e[u].add(v);
e[v].add(u);
}
int[] d1 = new int[n];
int[] d2 = new int[n];
int[] pre = new int[n];
bfs(0, d1, pre);
int u = 0;
for (int i = 0; i < n; i++)
if (d1[i] > d1[u]) u = i;
bfs(u, d2, pre);
int v = 0;
for (int i = 0; i < n; i++)
if (d2[i] > d2[v]) v = i;
List<Integer> list = new ArrayList<>();
while (v != -1) {
list.add(v);
v = pre[v];
}
if (list.size() % 2 == 1) return Arrays.asList(list.get(list.size() / 2));
else return Arrays.asList(list.get(list.size() / 2 - 1), list.get(list.size() / 2));
}
/**
* https://discuss.leetcode.com/topic/30956/two-o-n-solutions
*/
int n;
List<Integer>[] e;
int[] height1;
int[] height2;
int[] dp;
private void dfs4(int u, int parent) {
height1[u] = height2[u] = -Integer.MIN_VALUE / 10;
for (int v : e[u])
if (v != parent) {
dfs4(v, u);
int tmp = height1[v] + 1;
if (tmp > height1[u]) {
height2[u] = height1[u];
height1[u] = tmp;
} else if (tmp > height2[u]) {
height2[u] = tmp;
}
}
height1[u] = Math.max(height1[u], 0); // in case u is a leaf.
}
private void dfs4(int u, int parent, int acc) {
dp[u] = Math.max(height1[u], acc);
for (int v : e[u])
if (v != parent) {
int newAcc = Math.max(acc + 1, (height1[v] + 1 == height1[u] ? height2[u] : height1[u]) + 1);
dfs4(v, u, newAcc);
}
}
public List<Integer> findMinHeightTrees4(int n, int[][] edges) {
if (n <= 0) return new ArrayList<>();
if (n == 1) return Arrays.asList(0);
this.n = n;
e = new List[n];
for (int i = 0; i < n; i++)
e[i] = new ArrayList<>();
for (int[] pair : edges) {
int u = pair[0];
int v = pair[1];
e[u].add(v);
e[v].add(u);
}
height1 = new int[n];
height2 = new int[n];
dp = new int[n];
dfs4(0, -1);
dfs4(0, -1, 0);
int min = dp[0];
for (int i : dp)
if (i < min) min = i;
List<Integer> ans = new ArrayList<>();
for (int i = 0; i < n; i++)
if (dp[i] == min) ans.add(i);
return ans;
}
}