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

# Increasing Order Search Tree Python Solution

> Tested Python solution for LeetCode 897 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 897, [Easy](/catalog/easy). Topics: [Stack](/catalog/topics/stack), [Tree](/catalog/topics/tree), [Depth-First Search](/catalog/topics/depth-first-search), [Binary Search Tree](/catalog/topics/binary-search-tree), [Binary Tree](/catalog/topics/binary-tree). [View on LeetCode](https://leetcode.com/problems/increasing-order-search-tree/description/).

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

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

## Problem

Given the `root` of a binary search tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only one right child.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/11/17/ex1.jpg)

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

![Example 2](https://assets.leetcode.com/uploads/2020/11/17/ex2.jpg)

```
Input: root = [5,1,7]
Output: [1,null,5,null,7]
```

### Constraints

* The number of nodes in the given tree will be in the range \[1, 100].
* 0 \<= Node.val \<= 1000

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/increasing_order_search_tree/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/increasing_order_search_tree/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 pushed and popped exactly once
    # Space: O(n) for the node list plus the stack of left-spine ancestors
    def increasing_bst(self, root: TreeNode[int] | None) -> TreeNode[int] | None:
        nodes: list[TreeNode[int]] = []
        stack: list[TreeNode[int]] = []
        current = root

        while current or stack:
            while current:
                stack.append(current)
                current = current.left
            current = stack.pop()
            nodes.append(current)
            current = current.right

        for i, node in enumerate(nodes):
            node.left = None
            node.right = nodes[i + 1] if i + 1 < len(nodes) else None

        return nodes[0] if nodes else None
```

## Complexity

| Time | Space |
| - | - |
| O(n) each node is pushed and popped exactly once | O(n) for the node list plus the stack of left-spine ancestors |

## Tags


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