Description#
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.
Given a tree of n
nodes labelled from 0
to n - 1
, and an array of n - 1
edges
where edges[i] = [ai, bi]
indicates that there is an undirected edge between the two nodes ai
and bi
in the tree, you can choose any node of the tree as the root. When you select a node x
as the root, the result tree has height h
. Among all possible rooted trees, those with minimum height (i.e. min(h)
) are called minimum height trees (MHTs).
Return a list of all MHTs' root labels. You can return the answer in any order.
The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf.
Example 1:
![](https://spcdn.pages.dev/leetcode/problems/0310.Minimum%20Height%20Trees/images/e1.jpg)
Input: n = 4, edges = [[1,0],[1,2],[1,3]]
Output: [1]
Explanation: As shown, the height of the tree is 1 when the root is the node with label 1 which is the only MHT.
Example 2:
![](https://spcdn.pages.dev/leetcode/problems/0310.Minimum%20Height%20Trees/images/e2.jpg)
Input: n = 6, edges = [[3,0],[3,1],[3,2],[3,4],[5,4]]
Output: [3,4]
Constraints:
1 <= n <= 2 * 104
edges.length == n - 1
0 <= ai, bi < n
ai != bi
- All the pairs
(ai, bi)
are distinct. - The given input is guaranteed to be a tree and there will be no repeated edges.
Solutions#
Solution 1#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| class Solution:
def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]:
if n == 1:
return [0]
g = defaultdict(list)
degree = [0] * n
for a, b in edges:
g[a].append(b)
g[b].append(a)
degree[a] += 1
degree[b] += 1
q = deque(i for i in range(n) if degree[i] == 1)
ans = []
while q:
ans.clear()
for _ in range(len(q)):
a = q.popleft()
ans.append(a)
for b in g[a]:
degree[b] -= 1
if degree[b] == 1:
q.append(b)
return ans
|
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
| class Solution {
public List<Integer> findMinHeightTrees(int n, int[][] edges) {
if (n == 1) {
return Collections.singletonList(0);
}
List<Integer>[] g = new List[n];
Arrays.setAll(g, k -> new ArrayList<>());
int[] degree = new int[n];
for (int[] e : edges) {
int a = e[0], b = e[1];
g[a].add(b);
g[b].add(a);
++degree[a];
++degree[b];
}
Queue<Integer> q = new LinkedList<>();
for (int i = 0; i < n; ++i) {
if (degree[i] == 1) {
q.offer(i);
}
}
List<Integer> ans = new ArrayList<>();
while (!q.isEmpty()) {
ans.clear();
for (int i = q.size(); i > 0; --i) {
int a = q.poll();
ans.add(a);
for (int b : g[a]) {
if (--degree[b] == 1) {
q.offer(b);
}
}
}
}
return ans;
}
}
|
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
| class Solution {
public:
vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {
if (n == 1) return {0};
vector<vector<int>> g(n);
vector<int> degree(n);
for (auto& e : edges) {
int a = e[0], b = e[1];
g[a].push_back(b);
g[b].push_back(a);
++degree[a];
++degree[b];
}
queue<int> q;
for (int i = 0; i < n; ++i)
if (degree[i] == 1)
q.push(i);
vector<int> ans;
while (!q.empty()) {
ans.clear();
for (int i = q.size(); i > 0; --i) {
int a = q.front();
q.pop();
ans.push_back(a);
for (int b : g[a])
if (--degree[b] == 1)
q.push(b);
}
}
return ans;
}
};
|
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
| func findMinHeightTrees(n int, edges [][]int) []int {
if n == 1 {
return []int{0}
}
g := make([][]int, n)
degree := make([]int, n)
for _, e := range edges {
a, b := e[0], e[1]
g[a] = append(g[a], b)
g[b] = append(g[b], a)
degree[a]++
degree[b]++
}
var q []int
for i := 0; i < n; i++ {
if degree[i] == 1 {
q = append(q, i)
}
}
var ans []int
for len(q) > 0 {
ans = []int{}
for i := len(q); i > 0; i-- {
a := q[0]
q = q[1:]
ans = append(ans, a)
for _, b := range g[a] {
degree[b]--
if degree[b] == 1 {
q = append(q, b)
}
}
}
}
return ans
}
|