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

# Split BST Python Solution with Tests

> Tested Python solution for LeetCode 776 with 24 pytest cases. Generate a practice environment with lcpy.

LeetCode 776, [Medium](/catalog/medium). Topics: [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/split-bst/description/).

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

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

## Problem

Given the root of a binary search tree (BST) and an integer target, split the tree into two subtrees where one subtree has nodes that are all smaller or equal to the target value, while the other subtree has all nodes that are greater than the target value. It is not necessarily the case that the tree contains a node with the value target.

Additionally, most of the structure of the original tree should remain. Formally, for any child c with parent p in the original tree, if they are both in the same subtree after the split, then node c should still have the parent p.

Return an array of the two roots \[smaller, larger] of the two subtrees.

### Examples

![Example 1](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0700-0799/0776.Split%20BST/images/split-tree.jpg)

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

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

### Constraints

* The number of nodes in the tree is in the range \[1, 50].
* 0 \<= Node.val, target \<= 1000

## Solution

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

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


class Solution:
    # Time: O(log n) average, O(n) worst case (one node per tree level)
    # Space: O(log n) average, O(n) worst case (recursion stack)
    def split_bst(self, root: TreeNode[int] | None, target: int) -> list[TreeNode[int] | None]:
        if root is None:
            return [None, None]
        if root.val <= target:
            smaller, larger = self.split_bst(root.right, target)
            root.right = smaller
            return [root, larger]
        smaller, larger = self.split_bst(root.left, target)
        root.left = larger
        return [smaller, root]
```

## Complexity

| Time | Space |
| - | - |
| O(log n) average, O(n) worst case (one node per tree level) | O(log n) average, O(n) worst case (recursion stack) |

## Tags


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