Skip to content

Commit 6215520

Browse files
committed
solution to max-level-sum-binaryTree
1 parent ba8e6a8 commit 6215520

File tree

3 files changed

+47
-59
lines changed

3 files changed

+47
-59
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on.
88

99
Return the smallest level X such that the sum of all the values of nodes at level X is maximal.
10+
1011
**Example 1:**
1112

1213
```

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ package Solution
88
* Right *TreeNode
99
* }
1010
*/
11-
type TreeNode struct {
12-
Val int
13-
Left *TreeNode
14-
Right *TreeNode
15-
}
1611

1712
func maxLevelSum(root *TreeNode) int {
1813
tree := make(map[*TreeNode]int)

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

Lines changed: 46 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package Solution
22

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

911
type TreeNode struct {
@@ -12,77 +14,67 @@ type TreeNode struct {
1214
Right *TreeNode
1315
}
1416

15-
type BinaryTree struct {
16-
root *TreeNode
17-
}
17+
type SolutionFuncType func(*TreeNode) int
1818

19-
func (t *BinaryTree) insert(data int) *BinaryTree {
20-
if t.root == nil {
21-
t.root = &TreeNode{Val: data, Left: nil, Right: nil}
22-
} else {
23-
t.root.insert(data)
24-
}
25-
return t
19+
var SolutionFuncList = []SolutionFuncType{
20+
maxLevelSum,
2621
}
2722

28-
func (n *TreeNode) insert(data int) {
29-
if n == nil {
23+
var DefaultValue int = -1024
24+
25+
func InsertNodeToTree(tree *TreeNode, node *TreeNode) {
26+
if tree == nil {
27+
return
28+
}
29+
if tree.Val == DefaultValue {
30+
tree.Val = node.Val
3031
return
31-
} else if data <= n.Val {
32-
if n.Left == nil {
33-
n.Left = &TreeNode{Val: data, Left: nil, Right: nil}
34-
} else {
35-
n.Left.insert(data)
32+
}
33+
if node.Val > tree.Val {
34+
if tree.Right == nil {
35+
tree.Right = &TreeNode{Val: DefaultValue}
3636
}
37-
} else {
38-
if n.Right == nil {
39-
n.Right = &TreeNode{Val: data, Left: nil, Right: nil}
40-
} else {
41-
n.Right.insert(data)
37+
InsertNodeToTree(tree.Right, node)
38+
}
39+
if node.Val < tree.Val {
40+
if tree.Left == nil {
41+
tree.Left = &TreeNode{Val: DefaultValue}
4242
}
43+
InsertNodeToTree(tree.Left, node)
4344
}
4445
}
4546

46-
func main() {
47-
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
4854
}
4955

5056
func TestSolution(t *testing.T) {
5157
// 测试用例
58+
ast := assert.New(t)
59+
5260
// 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/
53-
tree1 := &BinaryTree{}
54-
tree1.insert(1).
55-
insert(7).
56-
insert(0).
57-
insert(7).
58-
insert(-8)
61+
treeNode := InitTree(1, 7, 0, 7, -8)
5962

60-
cases := []struct {
63+
var cases = []struct {
6164
name string
62-
inputs *BinaryTree
65+
inputs *TreeNode
6366
expect int
6467
}{
65-
{"TestCase1", tree1, 2},
68+
{"TestCase1", treeNode, 2},
6669
}
67-
68-
// 开始测试
69-
for i, c := range cases {
70-
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
71-
got := Solution(c.inputs)
72-
if !reflect.DeepEqual(got, c.expect) {
73-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
74-
c.expect, got, c.inputs)
75-
}
76-
})
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+
}
7779
}
7880
}
79-
80-
// 压力测试
81-
func BenchmarkSolution(b *testing.B) {
82-
83-
}
84-
85-
// 使用案列
86-
func ExampleSolution() {
87-
88-
}

0 commit comments

Comments
 (0)