@@ -2,8 +2,10 @@ package Solution
2
2
3
3
import (
4
4
"reflect"
5
- "strconv "
5
+ "runtime "
6
6
"testing"
7
+
8
+ "github.com/stretchr/testify/assert"
7
9
)
8
10
9
11
type TreeNode struct {
@@ -12,77 +14,67 @@ type TreeNode struct {
12
14
Right * TreeNode
13
15
}
14
16
15
- type BinaryTree struct {
16
- root * TreeNode
17
- }
17
+ type SolutionFuncType func (* TreeNode ) int
18
18
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 ,
26
21
}
27
22
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
30
31
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 }
36
36
}
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 }
42
42
}
43
+ InsertNodeToTree (tree .Left , node )
43
44
}
44
45
}
45
46
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
48
54
}
49
55
50
56
func TestSolution (t * testing.T ) {
51
57
// 测试用例
58
+ ast := assert .New (t )
59
+
52
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/
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 )
59
62
60
- cases : = []struct {
63
+ var cases = []struct {
61
64
name string
62
- inputs * BinaryTree
65
+ inputs * TreeNode
63
66
expect int
64
67
}{
65
- {"TestCase1" , tree1 , 2 },
68
+ {"TestCase1" , treeNode , 2 },
66
69
}
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
+ }
77
79
}
78
80
}
79
-
80
- // 压力测试
81
- func BenchmarkSolution (b * testing.B ) {
82
-
83
- }
84
-
85
- // 使用案列
86
- func ExampleSolution () {
87
-
88
- }
0 commit comments