Imagine you have a Binary Search Tree (BST) and need to split it into two separate trees based on a target value. This is like organizing a library where you want to separate books into two sections: one for books with ID numbers ≤ target, and another for books with ID numbers > target.

Given the root of a BST and an integer target, you need to split the tree into two subtrees:

  • Left subtree: Contains all nodes with values ≤ target
  • Right subtree: Contains all nodes with values > target

The challenge is to preserve the original tree structure as much as possible. If a parent-child relationship existed in the original tree and both nodes end up in the same subtree, that relationship should be maintained.

Important: The target value might not exist in the tree, but you still need to perform the split based on that value.

Return an array [leftRoot, rightRoot] where leftRoot is the root of the subtree with values ≤ target, and rightRoot is the root of the subtree with values > target.

Input & Output

example_1.py — Basic Split
$ Input: root = [4,2,6,1,3,5,8], target = 4
Output: [[4,2,null,1,3,null,null,null,null], [6,null,8,null,null]]
💡 Note: Node 4 and all nodes ≤ 4 (1,2,3,4) go to left tree. Nodes > 4 (6,8) go to right tree. Node 5 gets attached to the left tree as it's ≤ target.
example_2.py — Target Not in Tree
$ Input: root = [4,2,6,1,3,5,7], target = 2
Output: [[2,1,null,null,null], [4,3,6,null,null,5,7]]
💡 Note: Even though target 2 exists, we split at that value. Nodes ≤ 2 (1,2) form left tree, nodes > 2 (3,4,5,6,7) form right tree.
example_3.py — Single Node
$ Input: root = [1], target = 1
Output: [[1], []]
💡 Note: Single node with value 1, target is 1. Since 1 ≤ 1, the node goes to the left tree, right tree is empty.

Constraints

  • The number of nodes in the tree is in the range [0, 100]
  • 0 ≤ Node.val ≤ 100
  • 0 ≤ target ≤ 100
  • The tree is a valid BST (left subtree values < root < right subtree values)

Visualization

Tap to expand
Split BST - Optimal Solution INPUT Original BST: 4 2 6 1 3 5 8 root = [4,2,6,1,3,5,8] target = 4 Split at target = 4 Left: <= 4 | Right: > 4 ALGORITHM STEPS 1 Recursive Split Compare node with target 2 If node <= target Split right subtree recursively 3 If node > target Split left subtree recursively 4 Reconnect Subtrees Return [left, right] roots Split Process at node 4: 4 2 6 Cut edge to right child node.right = split(6)[0] FINAL RESULT Left Tree (<= 4) 4 2 1 3 Right Tree (> 4) 6 8 Output Arrays: Left: [4,2,null,1,3] Right: [6,null,8] OK - Split Complete! O(n) time, O(h) space Key Insight: BST property allows recursive splitting: if node <= target, node stays in left tree but its right subtree may need splitting. If node > target, node goes to right tree but left subtree may split. Reconnect children from recursive results to preserve structure. Time: O(n), Space: O(h). TutorialsPoint - Split BST | Optimal Recursive Solution
Asked in
Google 35 Meta 28 Amazon 22 Microsoft 18
23.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen