> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Binary Tree Zigzag Level Order Traversal

> Tested Python solution for LeetCode 103 with 13 pytest cases. Generate a practice environment with lcpy.

LeetCode 103, Medium. Topics: Tree, Breadth-First Search, Binary Tree. [View on LeetCode](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/description/).

Generate this problem as a practice environment: tested reference solution, 13 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 103   # by problem number
lcpy gen -s binary_tree_zigzag_level_order_traversal   # by problem name
```

## Problem

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

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg)

```
Input: root = [3,9,20,null,null,15,7]
Output: [[3],[20,9],[15,7]]
```

```
Input: root = [1]
Output: [[1]]
```

```
Input: root = []
Output: []
```

### Constraints

* The number of nodes in the tree is in the range \[0, 2000].
* -100 \<= Node.val \<= 100

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/binary_tree_zigzag_level_order_traversal/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/binary_tree_zigzag_level_order_traversal/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import deque

from leetcode_py import TreeNode


class Solution:
    # Time: O(n) — each node processed once
    # Space: O(n) — queue holds widest level
    def zigzag_level_order(self, root: TreeNode[int] | None) -> list[list[int]]:
        if not root:
            return []

        result: list[list[int]] = []
        queue: deque[TreeNode[int]] = deque([root])
        left_to_right = True

        while queue:
            level: deque[int] = deque()
            for _ in range(len(queue)):
                node = queue.popleft()
                if left_to_right:
                    level.append(node.val)
                else:
                    level.appendleft(node.val)
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
            result.append(list(level))
            left_to_right = not left_to_right

        return result
```

## Complexity

| Time                            | Space                           |
| ------------------------------- | ------------------------------- |
| O(n) — each node processed once | O(n) — queue holds widest level |

## Tags

[Grind](/catalog/grind), [NeetCode All](/catalog/neetcode).
