-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzigzag_order.go
39 lines (36 loc) · 882 Bytes
/
zigzag_order.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
package problem0103
import . "leetcodedaily/helpers/binarytree"
/*
Given the root of a binary tree, return the zigzag level order traversal of its nodes' values.
(i.e., from left to right, then right to left for the next level and alternate between).
*/
func zigzagLevelOrder(root *TreeNode) [][]int {
var res = [][]int{}
var vals []int
var cur, next []*TreeNode
var levelNum int
if root == nil {
return res
}
cur = []*TreeNode{root}
for levelNum = 0; len(cur) > 0; levelNum++ {
next = []*TreeNode{}
vals = make([]int, len(cur))
for i := 0; i < len(cur); i++ {
if levelNum%2 == 0 {
vals[i] = cur[i].Val
} else {
vals[len(cur)-1-i] = cur[i].Val
}
if cur[i].Left != nil {
next = append(next, cur[i].Left)
}
if cur[i].Right != nil {
next = append(next, cur[i].Right)
}
}
cur = next
res = append(res, vals)
}
return res
}