/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func zigzagLevelOrder(root *TreeNode) [][]int {
if root == nil {
return nil
}
queue := []*TreeNode{root}
level := 0
res := make([][]int, 0)
for ; len(queue) > 0; level++ {
tmpRes := make([]int, 0)
tmpArr := make([]*TreeNode, 0)
for _, item := range queue {
tmpRes = append(tmpRes, item.Val)
if item.Left != nil {
tmpArr = append(tmpArr, item.Left)
}
if item.Right != nil {
tmpArr = append(tmpArr, item.Right)
}
}
queue = tmpArr
if level%2 == 1 {
for i, n := 0, len(tmpRes); i < n/2; i++ {
tmpRes[i], tmpRes[n-1-i] = tmpRes[n-1-i], tmpRes[i]
}
}
res = append(res, tmpRes)
}
return res
}