forked from chenyiAlone/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinimumHeightTrees.java
53 lines (43 loc) · 1.42 KB
/
MinimumHeightTrees.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
package medium;
import java.util.*;
/**
* ClassName: MinimumHeightTrees.java
* Author: chenyiAlone
* Create Time: 2019/11/29 22:33
* Description: No.310 Minimum Height Trees
*/
public class MinimumHeightTrees {
private List<List<Integer>> graph;
private int[] hights;
private int maxH = Integer.MAX_VALUE;
private void dfs(int root, int cur, int parent, int depth) {
hights[root] = Math.max(hights[root], depth);
if (hights[root] > maxH) return;
for (int next : graph.get(cur)) {
if (next != parent) dfs(root, next, cur, depth + 1);
}
}
public List<Integer> findMinHeightTrees(int n, int[][] edges) {
graph = new ArrayList<>(n);
hights = new int[n];
for (int i = 0; i < n; i++) graph.add(new ArrayList<>());
for (int[] e : edges) {
graph.get(e[0]).add(e[1]);
graph.get(e[1]).add(e[0]);
}
for (int i = 0; i < n; i++) {
dfs(i, i, -1, 1);
maxH = Math.min(maxH, hights[i]);
}
Stack<Integer> ret = new Stack<>();
for (int i = 0; i < n; i++) {
// System.out.print(hights[i] + " ");
if (ret.isEmpty() || hights[i] == hights[ret.peek()]) ret.push(i);
else if (hights[i] < hights[ret.peek()]) {
ret.clear();
ret.push(i);
}
}
return ret;
}
}