8000 tmp:310 · kimi0230/LeetcodeGolang@df3e053 · GitHub
[go: up one dir, main page]

Skip to content

Commit df3e053

Browse files
committed
tmp:310
1 parent 5010be6 commit df3e053

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# [310. Minimum Height Trees](https://leetcode.com/problems/minimum-height-trees/)
2+
###### tags: `Medium` `Leetcode`
3+
4+
## 題目
5+
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.
6+
7+
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).
8+
9+
Return a list of all MHTs' root labels. You can return the answer in any order.
10+
11+
The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf.
12+
13+
14+
15+
Example 1:
16+
![](https://assets.leetcode.com/uploads/2020/09/01/e1.jpg)
17+
```
18+
Input: n = 4, edges = [[1,0],[1,2],[1,3]]
19+
Output: [1]
20+
Explanation: As shown, the height of the tree is 1 when the root is the node with label 1 which is the only MHT.
21+
```
22+
23+
Example 2:
24+
![](https://assets.leetcode.com/uploads/2020/09/01/e2.jpg)
25+
```
26+
Input: n = 6, edges = [[3,0],[3,1],[3,2],[3,4],[5,4]]
27+
Output: [3,4]
28+
```
29+
30+
Constraints:
31+
32+
* 1 <= n <= 2 * 104
33+
* edges.length == n - 1
34+
* 0 <= ai, bi < n
35+
* ai != bi
36+
* All the pairs (ai, bi) are distinct.
37+
* The given input is guaranteed to be a tree and there will be no repeated edges.
38+
39+
40+
## 題目大意
41+
輸入一個二叉樹, 計算出最小高度(root到left的最短距離)
42+
43+
## 解題思路
44+
用BFS來解. Queue
45+
46+
## 來源
47+
* https://leetcode.com/problems/minimum-height-trees/
48+
* https://leetcode-cn.com/problems/minimum-height-trees/
49+
50+
## 解答
51+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0310.Minimum-Height-Trees/main.go
52+
53+
```go
54+
55+
```

Leetcode/0310.Minimum-Height-Trees/main.go

Whitespace-only changes.

Leetcode/0310.Minimum-Height-Trees/main_test.go

Whitespace-only changes.

0 commit comments

Comments
 (0)
0