Solution Split BST
Problem Statement
Write a Recursive Approach to Split BST.
Given the root of a Binary Search Tree (BST) and an integer target, split the tree into two separate subtrees based on this target value.
- The first subtree should contain all nodes with values less than or equal to the target.
- The second subtree should contain all nodes with values greater than the target.
Even if the target value doesn't exist in the tree, the split should still happen based on the condition.
Important: The original parent-child relationships should be preserved as much as possible. That means if a node and its child both end up in the same subtree, they must still be connected in the same way as they were in the original tree.
Return an array of the two roots of the two subtrees in order.
Examples
Example 1:
-
Input:
-
Binary Search Tree:
4 / \ 2 6 / \ \ 1 3 7 -
Target: 5
-
-
Output:
-
Split BST 1:
-
Tree 1 (Greater):
6 \ 7 -
Tree 2 (Smaller):
4 / \ 2 5 / 1 \ 3
-
-
-
Explanation:
- In this example, the binary search tree is split based on the target value of 5.
- All nodes greater than the target value (6, 7) are moved to Tree 1, while all nodes smaller than or equal to the target value (4, 2, 5, 1, 3) are moved to Tree 2.
- The resulting Tree 1 contains nodes greater than 5, while Tree 2 contains nodes smaller than or equal to 5.
Example 2:
-
Input:
-
Binary Search Tree:
4 / \ 2 6 / \ \ 1 3 7 -
Target: 3
-
-
Output:
-
Split BST 2:
-
Tree 1 (Greater):
4 \ 6 \ 7 -
Tree 2 (Smaller):
2 / \ 1 3
-
-
-
Explanation:
- In this example, the binary search tree is split based on the target value of 3.
- All nodes greater than the target value (4, 6, 7) are moved to Tree 1, while all nodes smaller than or equal to the target value (2, 1, 3) are moved to Tree 2.
- The resulting Tree 1 contains nodes greater than 3, while Tree 2 contains nodes smaller than or equal to 3.
Example 3:
-
Input:
-
Binary Search Tree:
4 / \ 2 6 / \ \ 1 3 7 -
Target: 7
-
-
Output:
-
Split BST 3:
-
Tree 1 (Greater):
7 -
Tree 2 (Smaller):
4 / \ 2 6 / \ 1 3
-
-
-
Explanation:
- In this example, the binary search tree is split based on the target value of 7.
- Only the root node (7) is greater than the target value and is moved to Tree 1.
- The remaining nodes (4, 2, 6, 1, 3) are smaller than or equal to the target value and are moved to Tree 2.
- The resulting Tree 1 contains only the root node 7, while Tree 2 contains all other nodes from the original tree.
Constraints:
- The number of nodes in the tree is in the range
[1, 50]. 0 <= Node.val, target <= 1000
Solution
To split the BST, we need to traverse the tree recursively and split it based on the target value. The base case occurs when the current node is null, in which case we return null for both subtrees. The recursive case involves comparing the value of the current node with the target value and splitting the tree accordingly.
- If the current node's value is less than the target value, the entire left subtree is included in the resulting left subtree, and we need to split the right subtree recursively.
- If the current node's value is greater than or equal to the target value, the entire right subtree is included in the resulting right subtree, and we need to split the left subtree recursively.
The algorithm's overall approach is as follows:
- If the root is null, return null for both subtrees.
- If the root's value is less than the target value, split the right subtree recursively.
- If the root's value is greater than or equal to the target value, split the left subtree recursively.
The resulting subtrees are then connected to the appropriate sides of the root node, forming the split BST.
Code
Here is the code for this algorithm:
// class TreeNode {
// int val;
// TreeNode left;
// TreeNode right;
// TreeNode(int val) {
// this.val = val;
// }
// }
public class Solution {
public static TreeNode[] splitBST(TreeNode root, int target) {
if (root == null) {
return new TreeNode[] { null, null }; // Base case: empty subtree
}
if (root.val <= target) {
TreeNode[] rightSplit = splitBST(root.right, target); // Split right subtree recursively
root.right = rightSplit[0]; // Connect the resulting right subtree to the current node's right
return new TreeNode[] { root, rightSplit[1] }; // Return root and right subtree
} else {
TreeNode[] leftSplit = splitBST(root.left, target); // Split left subtree recursively
root.left = leftSplit[1]; // Connect the resulting left subtree to the current node's left
return new TreeNode[] { leftSplit[0], root }; // Return left subtree and root
}
}
public static void main(String[] args) {
// Example inputs
TreeNode root1 = new TreeNode(4);
root1.left = new TreeNode(2);
root1.right = new TreeNode(6);
root1.left.left = new TreeNode(1);
root1.left.right = new TreeNode(3);
root1.right.right = new TreeNode(7);
int target1 = 5;
TreeNode root2 = new TreeNode(3);
root2.left = new TreeNode(2);
root2.right = new TreeNode(4);
int target2 = 4;
TreeNode root3 = new TreeNode(2);
root3.left = new TreeNode(1);
root3.right = new TreeNode(3);
int target3 = 1;
// Split BSTs
TreeNode[] split1 = splitBST(root1, target1);
TreeNode[] split2 = splitBST(root2, target2);
TreeNode[] split3 = splitBST(root3, target3);
// Print results
System.out.println(
"Split BST 1: " + printBST(split1[0]) + ", " + printBST(split1[1])
);
System.out.println(
"Split BST 2: " + printBST(split2[0]) + ", " + printBST(split2[1])
);
System.out.println(
"Split BST 3: " + printBST(split3[0]) + ", " + printBST(split3[1])
);
}
// Helper method to print the BST
public static String printBST(TreeNode root) {
if (root == null) {
return "";
}
String left = printBST(root.left);
String right = printBST(root.right);
return left + " " + root.val + " " + right;
}
}
Space and Time Complexity
The time complexity of this algorithm is
The space complexity is
🤖 Don't fully get this? Learn it with Claude
Stuck on Solution Split BST? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **Solution Split BST** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **Solution Split BST** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Solution Split BST**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Solution Split BST**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.