> ## 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 Preorder Traversal Python Solution

> Tested Python solution for LeetCode 144 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 144, Easy. Topics: Stack, Tree, Depth-First Search, Binary Tree. [View on LeetCode](https://leetcode.com/problems/binary-tree-preorder-traversal/description/).

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

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

## Problem

Given the `root` of a binary tree, return the preorder traversal of its nodes' values.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png)

```
Input: root = [1,null,2,3]
Output: [1,2,3]
```

![Example 2](https://assets.leetcode.com/uploads/2024/08/29/tree_2.png)

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

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

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

### Constraints

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

**Follow up:** Recursive solution is trivial, could you do it iteratively?

## Solution

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

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


class Solution:
    # Time: O(n)
    # Space: O(h) where h is the height of the tree
    def preorder_traversal(self, root: TreeNode[int] | None) -> list[int]:
        if not root:
            return []

        result: list[int] = []
        stack: list[TreeNode[int]] = [root]

        while stack:
            node = stack.pop()
            result.append(node.val)

            # Push right first, then left (so left is processed first)
            if node.right:
                stack.append(node.right)
            if node.left:
                stack.append(node.left)

        return result
```

## Complexity

| Time | Space                                  |
| ---- | -------------------------------------- |
| O(n) | O(h) where h is the height of the tree |

## Tags

[NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode), [AlgoMaster 75](/catalog/algo-master-75).
