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

# Same Tree Python Solution with Tests

> Tested Python solution for LeetCode 100 with 11 pytest cases. Generate a practice environment with lcpy.

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

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

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

## Problem

Given the roots of two binary trees `p` and `q`, write a function to check if they are the same or not.

Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/12/20/ex1.jpg)

```
Input: p = [1,2,3], q = [1,2,3]
Output: true
```

![Example 2](https://assets.leetcode.com/uploads/2020/12/20/ex2.jpg)

```
Input: p = [1,2], q = [1,null,2]
Output: false
```

![Example 3](https://assets.leetcode.com/uploads/2020/12/20/ex3.jpg)

```
Input: p = [1,2,1], q = [1,1,2]
Output: false
```

### Constraints

* The number of nodes in both trees is in the range \[0, 100].
* -10^4 \<= Node.val \<= 10^4

## Solution

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

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


class Solution:
    # Time: O(min(m, n)) where m and n are the number of nodes in the two trees
    # Space: O(min(m, n)) for the recursion stack
    def is_same_tree(self, p: TreeNode[int] | None, q: TreeNode[int] | None) -> bool:
        # Base case: both nodes are None
        if p is None and q is None:
            return True

        # Base case: one node is None, the other is not
        if p is None or q is None:
            return False

        # Check if current nodes have the same value
        if p.val != q.val:
            return False

        # Recursively check left and right subtrees
        return self.is_same_tree(p.left, q.left) and self.is_same_tree(p.right, q.right)
```

## Complexity

| Time                                                                | Space                                |
| ------------------------------------------------------------------- | ------------------------------------ |
| O(min(m, n)) where m and n are the number of nodes in the two trees | O(min(m, n)) for the recursion stack |

## Tags

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