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

# Insert into a Binary Search Tree

> Tested Python solution for LeetCode 701 with 14 pytest cases. Generate a practice environment with lcpy.

LeetCode 701, Medium. Topics: Tree, Binary Search Tree, Binary Tree. [View on LeetCode](https://leetcode.com/problems/insert-into-a-binary-search-tree/description/).

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

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

## Problem

You are given the `root` node of a binary search tree (BST) and a `value` to insert into the tree. Return *the root node of the BST after the insertion*. It is **guaranteed** that the new value does not exist in the original BST.

**Notice** that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return **any of them**.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/10/05/insertbst.jpg)

```
Input: root = [4,2,7,1,3], val = 5
Output: [4,2,7,1,3,5]
Explanation: Another accepted tree is:

![Example 1 alt](https://assets.leetcode.com/uploads/2020/10/05/bst.jpg)
```

```
Input: root = [40,20,60,10,30,50,70], val = 25
Output: [40,20,60,10,30,50,70,null,null,25]
```

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

### Constraints

* The number of nodes in the tree will be in the range \[0, 10^4].
* -10^8 \<= Node.val \<= 10^8
* All the values `Node.val` are **unique**.
* -10^8 \<= val \<= 10^8
* It's **guaranteed** that `val` does not exist in the original BST.

## Solution

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

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


class Solution:
    # Time: O(h)
    # Space: O(1)
    def insert_into_bst(self, root: TreeNode[int] | None, val: int) -> TreeNode[int] | None:
        node = TreeNode(val)
        if root is None:
            return node

        current = root
        while True:
            if val < current.val:
                if current.left is None:
                    current.left = node
                    return root
                current = current.left
            else:
                if current.right is None:
                    current.right = node
                    return root
                current = current.right
```

## Complexity

| Time | Space |
| ---- | ----- |
| O(h) | O(1)  |

## Tags

[NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
