Skip to content

Commit 66aca24

Browse files
authored
Merge pull request #855 from 0xff-dev/310
Add solution and test-cases for problem 310
2 parents 6bcf4e9 + c93521d commit 66aca24

File tree

5 files changed

+68
-26
lines changed

5 files changed

+68
-26
lines changed

leetcode/301-400/0310.Minimum-Height-Trees/README.md

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
# [310.Minimum Height Trees][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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.
5+
6+
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).
7+
8+
Return a list of all **MHTs'** root labels. You can return the answer in **any order**.
9+
10+
The **height** of a rooted tree is the number of edges on the longest downward path between the root and a leaf.
711

8-
**Example 1:**
12+
**Example 1:**
13+
14+
![1](./e1.jpeg)
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: n = 4, edges = [[1,0],[1,2],[1,3]]
18+
Output: [1]
19+
Explanation: As shown, the height of the tree is 1 when the root is the node with label 1 which is the only MHT.
1320
```
1421

15-
## 题意
16-
> ...
22+
**Example 2:**
1723

18-
## 题解
24+
![2](./e2.jpeg)
1925

20-
### 思路1
21-
> ...
22-
Minimum Height Trees
23-
```go
2426
```
25-
27+
Input: n = 6, edges = [[3,0],[3,1],[3,2],[3,4],[5,4]]
28+
Output: [3,4]
29+
```
2630

2731
## 结语
2832

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,39 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int, edges [][]int) []int {
4+
if n == 1 {
5+
return []int{0}
6+
}
7+
adj := make([]map[int]struct{}, n)
8+
for i := 0; i < n; i++ {
9+
adj[i] = make(map[int]struct{})
10+
}
11+
for _, edge := range edges {
12+
a, b := edge[0], edge[1]
13+
adj[a][b] = struct{}{}
14+
adj[b][a] = struct{}{}
15+
}
16+
l := []int{}
17+
for i := 0; i < n; i++ {
18+
if len(adj[i]) == 1 {
19+
l = append(l, i)
20+
}
21+
}
22+
r := n
23+
for r > 2 {
24+
r -= len(l)
25+
nl := []int{}
26+
for _, lf := range l {
27+
var nei int
28+
for n := range adj[lf] {
29+
nei = n
30+
}
31+
delete(adj[nei], lf)
32+
if len(adj[nei]) == 1 {
33+
nl = append(nl, nei)
34+
}
35+
}
36+
l = nl
37+
}
38+
return l
539
}

leetcode/301-400/0310.Minimum-Height-Trees/Solution_test.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,34 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
n int
14+
edges [][]int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 4, [][]int{
18+
{1, 0}, {1, 2}, {1, 3},
19+
}, []int{1}},
20+
{"TestCase2", 6, [][]int{
21+
{3, 0}, {3, 1}, {3, 2}, {3, 4}, {5, 4},
22+
}, []int{3, 4}},
1923
}
2024

2125
// 开始测试
2226
for i, c := range cases {
2327
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
28+
got := Solution(c.n, c.edges)
2529
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
30+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
31+
c.expect, got, c.n, c.edges)
2832
}
2933
})
3034
}
3135
}
3236

33-
// 压力测试
37+
// 压力测试
3438
func BenchmarkSolution(b *testing.B) {
3539
}
3640

37-
// 使用案列
41+
// 使用案列
3842
func ExampleSolution() {
3943
}
Loading
Loading

0 commit comments

Comments
 (0)