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

# Binary Tree Paths Python Solution with Tests

> Tested Python solution for LeetCode 257 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 257, [Easy](/catalog/easy). Topics: [String](/catalog/topics/string), [Backtracking](/catalog/topics/backtracking), [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/binary-tree-paths/description/).

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

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

## Problem

Given the `root` of a binary tree, return all root-to-leaf paths in any order.

A **leaf** is a node with no children.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/03/12/paths-tree.jpg)

```
Input: root = [1,2,3,null,5]
Output: ["1->2->5","1->3"]
```

```
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/binary_tree_paths/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/binary_tree_paths/test_solution.py):

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


class Solution:
    # Time: O(n * d) where d is the average path length
    # Space: O(h) for the recursion stack, excluding the output
    def binary_tree_paths(self, root: TreeNode[int] | None) -> list[str]:
        paths: list[str] = []

        def dfs(node: TreeNode[int] | None, path: list[str]) -> None:
            if node is None:
                return
            path.append(str(node.val))
            if node.left is None and node.right is None:
                paths.append("->".join(path))
            else:
                dfs(node.left, path)
                dfs(node.right, path)
            path.pop()

        dfs(root, [])
        return paths
```

## Complexity

| Time | Space |
| - | - |
| O(n \* d) where d is the average path length | O(h) for the recursion stack, excluding the output |

## Tags


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