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

# Flatten Binary Tree to Linked List

> Tested Python solution for LeetCode 114 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 114, [Medium](/catalog/medium). Topics: [Linked List](/catalog/topics/linked-list), [Stack](/catalog/topics/stack), [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/flatten-binary-tree-to-linked-list/description/).

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

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

## Problem

Given the `root` of a binary tree, flatten the tree into a "linked list":

* The "linked list" should use the same `TreeNode` class where the `right` child pointer points to the next node in the list and the `left` child pointer is always `null`.
* The "linked list" should be in the same order as a **pre-order traversal** of the binary tree.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg)

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

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

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

### Constraints

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

**Follow up:** Can you flatten the tree in-place (with `O(1)` extra space)?

## Solution

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

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


class Solution:
    # Time: O(n), each node is visited a constant number of times
    # Space: O(1), pointers are rewired in place
    def flatten(self, root: TreeNode[int] | None) -> None:
        current = root
        while current is not None:
            if current.left is not None:
                predecessor = current.left
                while predecessor.right is not None:
                    predecessor = predecessor.right
                predecessor.right = current.right
                current.right = current.left
                current.left = None
            current = current.right
```

## Complexity

| Time | Space |
| - | - |
| O(n), each node is visited a constant number of times | O(1), pointers are rewired in place |

## Tags


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