-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path310.minimum-height-trees.java
More file actions
54 lines (52 loc) · 2.17 KB
/
310.minimum-height-trees.java
File metadata and controls
54 lines (52 loc) · 2.17 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
/*
* @lc app=leetcode id=310 lang=java
*
* [310] Minimum Height Trees
*/
// @lc code=start
class Solution {
/*
类似剥洋葱的方法,就是一层一层的褪去叶节点,最后剩下的一个或两个节点就是我们要求的最小高度树的根节点,
这种思路非常的巧妙,而且实现起来也不难,跟之前那到课程清单的题一样,我们需要建立一个图g,
是一个二维数组,其中g[i]是一个一维数组,保存了i节点可以到达的所有节点。我们开始将所有只有一个
连接边的节点(叶节点)都存入到一个队列queue中,然后我们遍历每一个叶节点,通过图来找到和其相连的节点,
并且在其相连节点的集合中将该叶节点删去,如果删完后此节点也也变成一个叶节点了,加入队列中,再下一轮删除。
那么我们删到什么时候呢,当节点数小于等于2时候停止,此时剩下的一个或两个节点就是我们要求的最小高度树的根节点
time: O(n) V= n E = n + 1
space: O(n)
*/
public List<Integer> findMinHeightTrees(int n, int[][] edges) {
List<Integer> res = new ArrayList<>();
if (n == 1) {
res.add(0);
return res;
}
Map<Integer, Set<Integer>> graph = new HashMap<>();
for (int[] edge : edges) {
graph.putIfAbsent(edge[0], new HashSet<>());
graph.putIfAbsent(edge[1], new HashSet<>());
graph.get(edge[0]).add(edge[1]);
graph.get(edge[1]).add(edge[0]);
}
Queue<Integer> q = new LinkedList<>();
for (int i = 0; i < n; ++i) {
if (graph.get(i).size() == 1) q.offer(i);
}
while (n > 2) {
int size = q.size();
n -= size;
for (int i = 0; i < size; ++i) {
int cur = q.poll();
for (int next : graph.get(cur)) {
graph.get(next).remove(cur);
if (graph.get(next).size() == 1) q.offer(next);
}
}
}
while (!q.isEmpty()) {
res.add(q.poll());
}
return res;
}
}
// @lc code=end