-
Notifications
You must be signed in to change notification settings - Fork 1
/
1609_even-odd-tree_test.go
66 lines (60 loc) · 1.38 KB
/
1609_even-odd-tree_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
62
63
64
65
66
package _5_binary_tree
import (
"github.com/smartystreets/goconvey/convey"
"math"
"testing"
)
func TestIsEvenOddTree(t *testing.T) {
convey.Convey("奇偶树", t, func() {
testCase := []struct {
root *TreeNode
target bool
}{
{
Ints2TreeNode([]int{2, 12, 8, 5, 9, NULL, NULL, 18, 16}),
false,
},
{
Ints2TreeNode([]int{1, 10, 4, 3, NULL, 7, 9, 12, 8, 6, NULL, NULL, 2}),
true,
},
}
for _, tst := range testCase {
rsp := isEvenOddTree(tst.root)
convey.So(rsp, convey.ShouldEqual, tst.target)
}
})
}
func isEvenOddTree(root *TreeNode) bool {
queue := []*TreeNode{root}
var even = true // 判断偶数层
for len(queue) > 0 {
var pre int
if !even {
pre = math.MaxInt32 // 初始值不一样
}
value := make([]int, len(queue))
for _ = range value {
node := queue[0]
queue = queue[1:]
if even { // 偶数下标 层上的所有节点的值都是 奇 整数,从左到右按顺序 严格递增
if node.Val <= pre || node.Val%2 == 0 {
return false
}
} else { // 奇数下标 层上的所有节点的值都是 偶 整数,从左到右按顺序 严格递减
if node.Val >= pre || node.Val%2 == 1 {
return false
}
}
pre = node.Val
if node.Left != nil {
queue = append(queue, node.Left)
}
if node.Right != nil {
queue = append(queue, node.Right)
}
}
even = !even
}
return true
}