-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.go
60 lines (55 loc) · 849 Bytes
/
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
package main
import "fmt"
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
type withNo struct {
*TreeNode
No int
}
func isCompleteTree(root *TreeNode) bool {
var nodes []withNo
preNo := 0
no := 1
nodes = append(nodes, withNo{root, no})
for len(nodes) > 0 {
node := nodes[0]
if preNo+1 != node.No {
return false
}
nodes = nodes[1:]
no++
if nil != node.Left {
nodes = append(nodes, withNo{node.Left, no})
}
no++
if nil != node.Right {
nodes = append(nodes, withNo{node.Right, no})
}
preNo = node.No
}
return true
}
func main() {
root := &TreeNode{
Val: 1,
Left: &TreeNode{
Val: 2,
Left: &TreeNode{
Val: 4,
},
Right: &TreeNode{
Val: 5,
},
},
Right: &TreeNode{
Val: 3,
Left: &TreeNode{
Val: 7,
},
},
}
fmt.Println(isCompleteTree(root))
}