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

# Smallest String Starting From Leaf

> Tested Python solution for LeetCode 988 with 12 pytest cases. Generate a practice environment with lcpy.

LeetCode 988, [Medium](/catalog/medium). 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/smallest-string-starting-from-leaf/description/).

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

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

## Problem

You are given the `root` of a binary tree where each node has a value in the range `[0, 25]` representing the letters `'a'` to `'z'`.

Return *the lexicographically smallest string that starts at a leaf of this tree and ends at the root*.

As a reminder, any shorter prefix of a string is lexicographically smaller.

* For example, `"ab"` is lexicographically smaller than `"aba"`.

A leaf of a node is a node that has no children.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2019/01/30/tree1.png)

```
Input: root = [0,1,2,3,4,3,4]
Output: "dba"
```

![Example 2](https://assets.leetcode.com/uploads/2019/01/30/tree2.png)

```
Input: root = [25,1,3,1,3,0,2]
Output: "adz"
```

![Example 3](https://assets.leetcode.com/uploads/2019/02/01/tree3.png)

```
Input: root = [2,2,1,null,1,0,null,0]
Output: "abc"
```

### Constraints

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

## Solution

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

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


class Solution:
    # Time: O(n^2) worst case for string building on a skewed tree
    # Space: O(n)
    def smallest_from_leaf(self, root: TreeNode[int] | None) -> str:
        best = ""

        def dfs(node: TreeNode[int] | None, path: str) -> None:
            nonlocal best
            if node is None:
                return
            path = chr(ord("a") + node.val) + path
            if node.left is None and node.right is None:
                if not best or path < best:
                    best = path
                return
            dfs(node.left, path)
            dfs(node.right, path)

        dfs(root, "")
        return best
```

## Complexity

| Time | Space |
| - | - |
| O(n^2) worst case for string building on a skewed tree | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


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