# 103. Binary Tree Zigzag Level Order Traversal

{% embed url="<https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/>" %}

设个bool值来判断要不要reverse，每次遍历一层就not一下

```python
class Solution:
    def zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]:
        if not root: return
        
        res = []
        q = collections.deque([root])
        
        zigzag = False
        while q:
            width = len(q)
            level = []
            for _ in range(width):
                node = q.popleft()
                level.append(node.val)
                if node.left: q.append(node.left)
                if node.right: q.append(node.right)
            if zigzag:
                level.reverse()
            zigzag = not zigzag
            res.append(level)
            
        return res
```
