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
Binary Search Tree Split ProcessTarget = 4: Split into ≤4 (left) and >4 (right)Original BST4261358Split Decision ProcessNode 4: 4 ≤ 4? ✓→ Goes to LEFT tree→ Split RIGHT subtree→ Connect resultsLEFT Tree (≤ 4)42513RIGHT Tree (> 4)68Structure preserved: parent-child relationships maintained within each result tree
Understanding the Visualization
1
Identify Split Point
Compare current node value with target to determine which result tree it belongs to
2
Recursive Decision
If node ≤ target, it goes left and we need to split its right subtree
3
Connect Results
Properly connect the recursive split results to maintain BST properties
4
Preserve Structure
Original parent-child relationships are maintained within each result tree
Key Takeaway
🎯 Key Insight: Use recursion to make split decisions at each node, leveraging BST properties to determine which subtree needs further processing while preserving the original tree structure.
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