-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinary_tree_preorder_traversal_test.go
61 lines (55 loc) · 1.12 KB
/
binary_tree_preorder_traversal_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"reflect"
"testing"
"algo/structure"
)
//Runtime: 4 ms, faster than 12.62% of Go online submissions for Binary Tree Preorder Traversal.
//Memory Usage: 2.1 MB, less than 40.97% of Go online submissions for Binary Tree Preorder Traversal.
func Test_preorderTraversal(t *testing.T) {
type args struct {
tree []int
}
tests := []struct {
name string
args args
want []int
}{
{
name: "144-1",
args: args{
tree: []int{1, structure.NULL, 2, 3},
},
want: []int{1, 2, 3},
},
{
name: "144-2",
args: args{
tree: []int{},
},
want: nil,
},
{
name: "144-3",
args: args{
tree: []int{1},
},
want: []int{1},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
root := structure.Int2TreeNode(tt.args.tree)
if got := preorderTraversal(root); !reflect.DeepEqual(got, tt.want) {
t.Errorf("preorderTraversal() = %v, want %v", got, tt.want)
}
})
}
}
func Benchmark_preorderTraversal(b *testing.B) {
tree := []int{4, 2, 5, 1, 6, 3, 7}
root := structure.Int2TreeNode(tree)
for i := 0; i < b.N; i++ {
preorderTraversal(root)
}
}