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

# Find Leaves of Binary Tree Python Solution

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

LeetCode 366, [Medium](/catalog/medium). 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/find-leaves-of-binary-tree/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 366   # by problem number
lcpy gen -s find_leaves_of_binary_tree   # by problem name
```

## Problem

Given the `root` of a binary tree, collect a tree's nodes as if you were doing this:

* Collect all the leaf nodes.
* Remove all the leaf nodes.
* Repeat until the tree is empty.

### Examples

![Example 1](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0300-0399/0366.Find%20Leaves%20of%20Binary%20Tree/images/remleaves-tree.jpg)

```
Input: root = [1,2,3,4,5]
Output: [[4,5,3],[2],[1]]
Explanation:
[[3,5,4],[2],[1]] and [[3,4,5],[2],[1]] are also considered correct answers since per each level it does not matter the order on which elements are returned.
```

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

### Constraints

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

## Solution

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

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


class Solution:
    # Time: O(n) — every node visited once
    # Space: O(h) — recursion depth equals tree height
    def find_leaves(self, root: TreeNode[int] | None) -> list[list[int]]:
        result: list[list[int]] = []

        def height(node: TreeNode[int] | None) -> int:
            if node is None:
                return 0
            h = 1 + max(height(node.left), height(node.right))
            while len(result) < h:
                result.append([])
            result[h - 1].append(node.val)
            return h

        height(root)
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(n) — every node visited once | O(h) — recursion depth equals tree height |

## Tags

[NeetCode All](/catalog/neetcode).


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