### 103.Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).

For example:

``````Given binary tree [3,9,20,null,null,15,7],
3
/ \
9  20
/  \
15   7``````

return its zigzag level order traversal as:

``````[
[3],
[20,9],
[15,7]
]``````

### analyze

``````/**
* Definition for a binary tree node.
* function TreeNode(val) {
*     this.val = val;
*     this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[][]}
*/
var zigzagLevelOrder = function(root) {
const printArr = []
if (!root) return printArr
const list = []
list.push({ level: 0, node: root })
while(list.length > 0) {
const { level, node } = list.shift()
if (!printArr[level]) {
printArr[level] = []
}

if (level % 2 === 0) {
printArr[level].push(node.val)
} else {
printArr[level].unshift(node.val)
}

node.left && list.push({ level: level + 1, node: node.left })
node.right && list.push({ level: level + 1, node: node.right })
}

return printArr
}``````

102、107、199