# 103 Binary Tree Zigzag Level Order Traversal

Original Description
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).

• 要求： 给一个binary tree， do zigzag level traversal.

• 返回：
traversal 返回的数组

• 例子：
• input: [3,9,20,null,null,15,7] 。 3 。 /
。 9 20 。。 /
。。15 7
• output: [ [3], [20,9], [15,7]]

## Solution

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution(object):
def zigzagLevelOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""

result = []
if not root:
return result
curr_level = [root]
need_reverse = False
while curr_level:
level_result = []
next_level = []
for temp in curr_level:
level_result.append(temp.val)
if temp.left:
next_level.append(temp.left)
if temp.right:
next_level.append(temp.right)
if need_reverse:
level_result.reverse()
need_reverse = False
else:
need_reverse = True
result.append(level_result)
curr_level = next_level
return result