Problem :

https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/


My Solution :

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
def zigzagLevelOrder(self, root: 'TreeNode') -> 'List[List[int]]':
rev = False
ret = []
if root:
queue = [root]
while queue:
level_ret = []
level_queue = []
for node in queue:
level_ret.append(node.val)
if node.left:
level_queue.append(node.left)
if node.right:
level_queue.append(node.right)
if rev:
ret.append(level_ret[::-1])
else:
ret.append(level_ret)
rev = not rev
queue = level_queue
return ret