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

# Leaf-Similar Trees Python Solution with Tests

> Tested Python solution for LeetCode 872 with 14 pytest cases. Generate a practice environment with lcpy.

LeetCode 872, [Easy](/catalog/easy). Topics: [Tree](/catalog/topics/tree), [Depth-First Search](/catalog/topics/depth-first-search), [Binary Tree](/catalog/topics/binary-tree). [View on LeetCode](https://leetcode.com/problems/leaf-similar-trees/description/).

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

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

## Problem

Consider all the leaves of a binary tree, from left to right order, the values of those leaves form a \<strong>leaf value sequence\</strong>.\</p>

\<p>For example, in the given tree above, the leaf value sequence is \<code>(6, 7, 4, 9, 8)\</code>.\</p>

\<p>Two binary trees are considered \<em>leaf-similar\</em> if their leaf value sequence is the same.\</p>

\<p>Return \<code>true\</code> if and only if the two given trees with head nodes \<code>root1\</code> and \<code>root2\</code> are leaf-similar.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/09/03/leaf-similar-1.jpg)

```
Input: root1 = [3,5,1,6,2,9,8,null,null,7,4], root2 = [3,5,1,6,7,4,2,null,null,null,null,null,null,9,8]
Output: true
```

![Example 2](https://assets.leetcode.com/uploads/2020/09/03/leaf-similar-2.jpg)

```
Input: root1 = [1,2,3], root2 = [1,3,2]
Output: false
```

### Constraints

* The number of nodes in each tree will be in the range \[1, 200].
* Both of the given trees will have values in the range \[0, 200].

## Solution

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

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


class Solution:
    # Time: O(n + m)
    # Space: O(n + m)
    def leaf_similar(self, root1: TreeNode[int] | None, root2: TreeNode[int] | None) -> bool:
        def leaves(root: TreeNode[int] | None) -> list[int]:
            values: list[int] = []
            stack: list[TreeNode[int] | None] = [root]
            while stack:
                node = stack.pop()
                if not node:
                    continue
                if not node.left and not node.right:
                    values.append(node.val)
                stack.append(node.left)
                stack.append(node.right)
            return values

        return leaves(root1) == leaves(root2)
```

## Complexity

| Time | Space |
| - | - |
| O(n + m) | O(n + m) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.