Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 8ef42c7

Browse files
aQuaaQua
aQua
authored and
aQua
committed
515 added
1 parent 6516d4f commit 8ef42c7

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# [515. Find Largest Value in Each Tree Row](https://leetcode.com/problems/find-largest-value-in-each-tree-row/)
2+
3+
## 题目
4+
5+
You need to find the largest value in each row of a binary tree.
6+
7+
Example:
8+
9+
```text
10+
Input:
11+
12+
1
13+
/ \
14+
3 2
15+
/ \ \
16+
5 3 9
17+
18+
Output: [1, 3, 9]
19+
```
20+
21+
## 解题思路
22+
23+
见程序注释
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package Problem0515
2+
3+
import (
4+
"github.com/aQuaYi/LeetCode-in-Go/kit"
5+
)
6+
7+
type TreeNode = kit.TreeNode
8+
9+
func largestValues(root *TreeNode) []int {
10+
res := []int{1, 3, 9}
11+
12+
return res
13+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package Problem0515
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/aQuaYi/LeetCode-in-Go/kit"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
// tcs is testcase slice
13+
var tcs = []struct {
14+
pre, in []int
15+
ans []int
16+
}{
17+
18+
{
19+
[]int{1, 3, 5, 6, 2, 9},
20+
[]int{5, 3, 6, 1, 2, 9},
21+
[]int{1, 3, 9},
22+
},
23+
24+
// 可以有多个 testcase
25+
}
26+
27+
func Test_largestValues(t *testing.T) {
28+
ast := assert.New(t)
29+
30+
for _, tc := range tcs {
31+
fmt.Printf("~~%v~~\n", tc)
32+
root := kit.PreIn2Tree(tc.pre, tc.in)
33+
ast.Equal(tc.ans, largestValues(root), "输入:%v", tc)
34+
}
35+
}
36+
37+
func Benchmark_largestValues(b *testing.B) {
38+
for i := 0; i < b.N; i++ {
39+
for _, tc := range tcs {
40+
root := kit.PreIn2Tree(tc.pre, tc.in)
41+
largestValues(root)
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)