Skip to content

Commit 449ff74

Browse files
Split BST - Accepted
1 parent 82d43ad commit 449ff74

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ My accepted leetcode solutions to some of the common interview problems.
308308
- [Binary Tree Longest Consecutive Sequence II](problems/src/tree/BinaryTreeLongestConsecutiveSequenceII.java) (Medium)
309309
- [Minimum Absolute Difference in BST](problems/src/tree/MinimumAbsoluteDifferenceInBST.java) (Medium)
310310
- [Equal Tree Partition](problems/src/tree/EqualTreePartition.java) (Medium)
311+
- [Split BST](problems/src/tree/SplitBST.java) (Medium)
311312

312313
#### [Two Pointers](problems/src/two_pointers)
313314

problems/src/tree/SplitBST.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package tree;
2+
3+
/**
4+
* Created by gouthamvidyapradhan on 01/05/2018.
5+
* Given a Binary Search Tree (BST) with root node root, and a target value V, split the tree into two subtrees
6+
* where one subtree has nodes that are all smaller or equal to the target value, while the other subtree has all
7+
* nodes that are greater than the target value. It's not necessarily the case that the tree contains a node with
8+
* value V.
9+
10+
Additionally, most of the structure of the original tree should remain. Formally, for any child C with parent P in
11+
the original tree, if they are both in the same subtree after the split, then node C should still have the parent P.
12+
13+
You should output the root TreeNode of both subtrees after splitting, in any order.
14+
15+
Example 1:
16+
17+
Input: root = [4,2,6,1,3,5,7], V = 2
18+
Output: [[2,1],[4,3,6,null,null,5,7]]
19+
Explanation:
20+
Note that root, output[0], and output[1] are TreeNode objects, not arrays.
21+
22+
The given tree [4,2,6,1,3,5,7] is represented by the following diagram:
23+
24+
4
25+
/ \
26+
2 6
27+
/ \ / \
28+
1 3 5 7
29+
30+
while the diagrams for the outputs are:
31+
32+
4
33+
/ \
34+
3 6 and 2
35+
/ \ /
36+
5 7 1
37+
Note:
38+
39+
The size of the BST will not exceed 50.
40+
The BST is always valid and each node's value is different.
41+
42+
Solution: O(N) if a current node is <= to key then the current node and its child nodes form the left sub-tree. Split
43+
the right node further recursively
44+
*/
45+
public class SplitBST {
46+
47+
public static class TreeNode {
48+
int val;
49+
TreeNode left;
50+
TreeNode right;
51+
TreeNode(int x) { val = x; }
52+
}
53+
54+
/**
55+
* Main method
56+
* @param args
57+
* @throws Exception
58+
*/
59+
public static void main(String[] args) throws Exception{
60+
TreeNode root = new TreeNode(4);
61+
root.left = new TreeNode(2);
62+
root.left.left = new TreeNode(1);
63+
root.left.right = new TreeNode(3);
64+
root.right = new TreeNode(6);
65+
root.right.left = new TreeNode(5);
66+
root.right.right = new TreeNode(7);
67+
root.right.right.right = new TreeNode(9);
68+
TreeNode[] result = new SplitBST().splitBST(root, 3);
69+
}
70+
71+
public TreeNode[] splitBST(TreeNode root, int V) {
72+
if(root == null){
73+
return new TreeNode[] {null, null};
74+
} else{
75+
TreeNode[] result = new TreeNode[2];
76+
if(root.val <= V){
77+
result[0] = root;
78+
TreeNode[] right = splitBST(root.right, V);
79+
root.right = right[0];
80+
result[1] = right[1];
81+
return result;
82+
} else{
83+
TreeNode[] left = splitBST(root.left, V);
84+
root.left = left[1];
85+
result[0] = left[0];
86+
result[1] = root;
87+
return result;
88+
}
89+
}
90+
}
91+
92+
}

0 commit comments

Comments
 (0)