Skip to content

Commit a5b7a6c

Browse files
authored
Merge pull request #859 from 0xff-dev/671
Add solution and test-cases for problem 671
2 parents c9408f7 + eea84ff commit a5b7a6c

File tree

5 files changed

+69
-23
lines changed

5 files changed

+69
-23
lines changed

leetcode/601-700/0671.Second-Minimum-Node-In-a-Binary-Tree/README.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
# [671.Second Minimum Node In a Binary Tree][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+
Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly `two` or `zero` sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes. More formally, the property `root.val = min(root.left.val, root.right.val)` always holds.
5+
6+
Given such a binary tree, you need to output the **second minimum** value in the set made of all the nodes' value in the whole tree.
7+
8+
If no such second minimum value exists, output -1 instead.
79

8-
**Example 1:**
10+
**Example 1:**
11+
12+
![1](./smbt1.jpeg)
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: root = [2,2,5,null,null,5,7]
16+
Output: 5
17+
Explanation: The smallest value is 2, the second smallest value is 5.
1318
```
1419

15-
## 题意
16-
> ...
20+
**Example 2:**
1721

18-
## 题解
22+
![2](./smbt2.jpeg)
1923

20-
### 思路1
21-
> ...
22-
Second Minimum Node In a Binary Tree
23-
```go
2424
```
25-
25+
Input: root = [2,2,2]
26+
Output: -1
27+
Explanation: The smallest value is 2, but there isn't any second smallest value.
28+
```
2629

2730
## 结语
2831

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
type TreeNode struct {
6+
Val int
7+
Left, Right *TreeNode
8+
}
9+
10+
// 不是最好的解法
11+
func Solution(root *TreeNode) int {
12+
queue := []*TreeNode{root}
13+
v := make([]int, 0)
14+
uv := map[int]struct{}{}
15+
16+
for len(queue) > 0 {
17+
nq := make([]*TreeNode, 0)
18+
for _, cur := range queue {
19+
if _, ok := uv[cur.Val]; !ok {
20+
v = append(v, cur.Val)
21+
uv[cur.Val] = struct{}{}
22+
}
23+
if cur.Left != nil {
24+
nq = append(nq, cur.Left)
25+
}
26+
if cur.Right != nil {
27+
nq = append(nq, cur.Right)
28+
}
29+
}
30+
queue = nq
31+
}
32+
sort.Ints(v)
33+
if len(v) < 2 {
34+
return -1
35+
}
36+
return v[1]
537
}

leetcode/601-700/0671.Second-Minimum-Node-In-a-Binary-Tree/Solution_test.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,23 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs *TreeNode
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", &TreeNode{
17+
Val: 2,
18+
Left: &TreeNode{Val: 2},
19+
Right: &TreeNode{
20+
Val: 5,
21+
Left: &TreeNode{Val: 5},
22+
Right: &TreeNode{Val: 7},
23+
},
24+
}, 5},
25+
{"TestCase", &TreeNode{
26+
Val: 2,
27+
Left: &TreeNode{Val: 2},
28+
Right: &TreeNode{Val: 2},
29+
}, -1},
1930
}
2031

2132
// 开始测试
@@ -30,10 +41,10 @@ func TestSolution(t *testing.T) {
3041
}
3142
}
3243

33-
// 压力测试
44+
// 压力测试
3445
func BenchmarkSolution(b *testing.B) {
3546
}
3647

37-
// 使用案列
48+
// 使用案列
3849
func ExampleSolution() {
3950
}
Loading
Loading

0 commit comments

Comments
 (0)