Skip to content

Commit ea8c399

Browse files
author
Kyle Liu
committed
Merge branch 'develop' into master
2 parents 4fce2f3 + d7cebed commit ea8c399

File tree

4 files changed

+127
-30
lines changed

4 files changed

+127
-30
lines changed

leetcode/1101-1200/1161.Maximum-Level-Sum-of-a-Binary-Tree/README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,24 @@
44
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
55
66
## Description
7+
Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on.
8+
9+
Return the smallest level X such that the sum of all the values of nodes at level X is maximal.
710

811
**Example 1:**
912

1013
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
14+
Input: root = [1,7,0,7,-8,null,null]
15+
Output: 2
16+
Explanation:
17+
Level 1 sum = 1.
18+
Level 2 sum = 7 + 0 = 7.
19+
Level 3 sum = 7 + -8 = -1.
20+
So we return the level with the maximum sum which is level 2.
1321
```
1422

23+
Link to question: https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/
24+
1525
## 题意
1626
> ...
1727
Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,53 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
/**
4+
* Definition for a binary tree node.
5+
* type TreeNode struct {
6+
* Val int
7+
* Left *TreeNode
8+
* Right *TreeNode
9+
* }
10+
*/
11+
12+
func maxLevelSum(root *TreeNode) int {
13+
tree := make(map[*TreeNode]int)
14+
tree[root] = 0
15+
var treeList []*TreeNode
16+
treeList = append(treeList, root)
17+
sum := root.Val
18+
l := 0
19+
var sumList []int
20+
level := tree[root]
21+
for len(treeList) > 0 {
22+
r := treeList[0]
23+
treeList = treeList[1:]
24+
25+
if tree[r] > level {
26+
sumList = append(sumList, sum)
27+
sum = 0
28+
level = tree[r]
29+
}
30+
sum += r.Val
31+
32+
if r.Left != nil {
33+
treeList = append(treeList, r.Left)
34+
tree[r.Left] = level + 1
35+
}
36+
37+
if r.Right != nil {
38+
treeList = append(treeList, r.Right)
39+
tree[r.Right] = level + 1
40+
}
41+
42+
}
43+
44+
sumList = append(sumList, sum)
45+
max := sumList[0]
46+
for i, j := range sumList {
47+
if j > max {
48+
max = j
49+
l = i
50+
}
51+
}
52+
return l + 1
553
}

leetcode/1101-1200/1161.Maximum-Level-Sum-of-a-Binary-Tree/Solution_test.go

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,79 @@ package Solution
22

33
import (
44
"reflect"
5-
"strconv"
5+
"runtime"
66
"testing"
7+
8+
"github.com/stretchr/testify/assert"
79
)
810

9-
func TestSolution(t *testing.T) {
10-
// 测试用例
11-
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
15-
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
19-
}
11+
type TreeNode struct {
12+
Val int
13+
Left *TreeNode
14+
Right *TreeNode
15+
}
2016

21-
// 开始测试
22-
for i, c := range cases {
23-
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25-
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
28-
}
29-
})
30-
}
17+
type SolutionFuncType func(*TreeNode) int
18+
19+
var SolutionFuncList = []SolutionFuncType{
20+
maxLevelSum,
3121
}
3222

33-
// 压力测试
34-
func BenchmarkSolution(b *testing.B) {
23+
var DefaultValue int = -1024
3524

25+
func InsertNodeToTree(tree *TreeNode, node *TreeNode) {
26+
if tree == nil {
27+
return
28+
}
29+
if tree.Val == DefaultValue {
30+
tree.Val = node.Val
31+
return
32+
}
33+
if node.Val > tree.Val {
34+
if tree.Right == nil {
35+
tree.Right = &TreeNode{Val: DefaultValue}
36+
}
37+
InsertNodeToTree(tree.Right, node)
38+
}
39+
if node.Val < tree.Val {
40+
if tree.Left == nil {
41+
tree.Left = &TreeNode{Val: DefaultValue}
42+
}
43+
InsertNodeToTree(tree.Left, node)
44+
}
45+
}
46+
47+
func InitTree(values ...int) (root *TreeNode) {
48+
rootNode := TreeNode{Val: DefaultValue, Right: nil, Left: nil}
49+
for _, value := range values {
50+
node := TreeNode{Val: value}
51+
InsertNodeToTree(&rootNode, &node)
52+
}
53+
return &rootNode
3654
}
3755

38-
// 使用案列
39-
func ExampleSolution() {
56+
func TestSolution(t *testing.T) {
57+
// 测试用例
58+
ast := assert.New(t)
4059

60+
// The original Problem requires the binary tree constructed from array. Please refer to test cases produced at Leetcode problem https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/
61+
treeNode := InitTree(1, 7, 0, 7, -8)
62+
63+
var cases = []struct {
64+
name string
65+
inputs *TreeNode
66+
expect int
67+
}{
68+
{"TestCase1", treeNode, 2},
69+
}
70+
for _, f := range SolutionFuncList {
71+
for _, c := range cases {
72+
t.Run(c.name, func(t *testing.T) {
73+
actual := f(c.inputs)
74+
ast.Equal(c.expect, actual,
75+
"func: %v case: %v ",
76+
runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), c.name)
77+
})
78+
}
79+
}
4180
}

test.sh

100644100755
File mode changed.

0 commit comments

Comments
 (0)