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

# Symmetric Tree Python Solution with Tests

> Tested Python solution for LeetCode 101 with 16 pytest cases. Generate a practice environment with lcpy.

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

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

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

## Problem

Given the `root` of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).

### Examples

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

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

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

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

### Constraints

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

**Follow up:** Could you solve it both recursively and iteratively?

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from leetcode_py import TreeNode


class Solution:
    # Time: O(n) — each node visited once
    # Space: O(h) recursion stack, h = tree height
    def is_symmetric(self, root: TreeNode[int] | None) -> bool:
        def mirror(a: TreeNode[int] | None, b: TreeNode[int] | None) -> bool:
            if a is None or b is None:
                return a is b
            return a.val == b.val and mirror(a.left, b.right) and mirror(a.right, b.left)

        return mirror(root.left, root.right) if root else True
```

## Complexity

| Time                          | Space                                 |
| ----------------------------- | ------------------------------------- |
| O(n) — each node visited once | O(h) recursion stack, h = tree height |

## Tags

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