> ## 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.

# Invert Binary Tree Python Solution with Tests

> Tested Python solution for LeetCode 226 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 226, Easy. Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree. [View on LeetCode](https://leetcode.com/problems/invert-binary-tree/description/).

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

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

## Problem

Given the `root` of a binary tree, invert the tree, and return its root.

### Examples

```
Input: root = [4,2,7,1,3,6,9]
Output: [4,7,2,9,6,3,1]
```

```
Input: root = [2,1,3]
Output: [2,3,1]
```

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

### Constraints

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

## Solution

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

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

from leetcode_py import TreeNode

# Note: "Fringe" is the general CS term for the data structure holding nodes to be explored.
# Stack (LIFO) → DFS, Queue (FIFO) → BFS, Priority Queue → A*/Best-first search


class Solution:
    # DFS recursive
    # Time: O(n)
    # Space: O(h) where h is height of tree
    def invert_tree(self, root: TreeNode[int] | None) -> TreeNode[int] | None:
        if not root:
            return None

        root.left, root.right = self.invert_tree(root.right), self.invert_tree(root.left)
        return root


class SolutionDFS:
    # DFS iterative
    # Time: O(n)
    # Space: O(h) where h is height of tree
    def invert_tree(self, root: TreeNode[int] | None) -> TreeNode[int] | None:
        if not root:
            return None

        stack: list[TreeNode[int] | None] = [root]
        while stack:
            node = stack.pop()
            if node is None:
                continue
            node.left, node.right = node.right, node.left

            stack.append(node.left)
            stack.append(node.right)

        return root


class SolutionBFS:
    # Time: O(n)
    # Space: O(w) where w is maximum width of tree
    def invert_tree(self, root: TreeNode[int] | None) -> TreeNode[int] | None:
        if not root:
            return None

        queue: deque[TreeNode[int] | None] = deque([root])
        while queue:
            node = queue.popleft()
            if node is None:
                continue
            node.left, node.right = node.right, node.left

            queue.append(node.left)
            queue.append(node.right)

        return root
```

## Complexity

| Time | Space                          |
| ---- | ------------------------------ |
| O(n) | O(h) where h is height of tree |

## Tags

[Grind 75](/catalog/grind-75), [Grind](/catalog/grind), [Blind 75](/catalog/blind-75), [NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
