Skip to content

Add solution and test-cases for problem 310 #855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions leetcode/301-400/0310.Minimum-Height-Trees/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
# [310.Minimum Height Trees][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## 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:**
**Example 1:**

![1](./e1.jpeg)

```
Input: a = "11", b = "1"
Output: "100"
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:**

## 题解
![2](./e2.jpeg)

### 思路1
> ...
Minimum Height Trees
```go
```

Input: n = 6, edges = [[3,0],[3,1],[3,2],[3,4],[5,4]]
Output: [3,4]
```

## 结语

Expand Down
38 changes: 36 additions & 2 deletions leetcode/301-400/0310.Minimum-Height-Trees/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(n int, edges [][]int) []int {
if n == 1 {
return []int{0}
}
adj := make([]map[int]struct{}, n)
for i := 0; i < n; i++ {
adj[i] = make(map[int]struct{})
}
for _, edge := range edges {
a, b := edge[0], edge[1]
adj[a][b] = struct{}{}
adj[b][a] = struct{}{}
}
l := []int{}
for i := 0; i < n; i++ {
if len(adj[i]) == 1 {
l = append(l, i)
}
}
r := n
for r > 2 {
r -= len(l)
nl := []int{}
for _, lf := range l {
var nei int
for n := range adj[lf] {
nei = n
}
delete(adj[nei], lf)
if len(adj[nei]) == 1 {
nl = append(nl, nei)
}
}
l = nl
}
return l
}
24 changes: 14 additions & 10 deletions leetcode/301-400/0310.Minimum-Height-Trees/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,34 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
n int
edges [][]int
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 4, [][]int{
{1, 0}, {1, 2}, {1, 3},
}, []int{1}},
{"TestCase2", 6, [][]int{
{3, 0}, {3, 1}, {3, 2}, {3, 4}, {5, 4},
}, []int{3, 4}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.n, c.edges)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.n, c.edges)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.