##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/776.html

# 776. Split BST

Medium

## Description

Given a Binary Search Tree (BST) with root node root, and a target value V, 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’s not necessarily the case that the tree contains a node with value V.

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.

You should output the root TreeNode of both subtrees after splitting, in any order.

Example 1:

Input: root = [4,2,6,1,3,5,7], V = 2
Output: [[2,1],[4,3,6,null,null,5,7]]
Explanation:
Note that root, output, and output are TreeNode objects, not arrays.

The given tree [4,2,6,1,3,5,7] is represented by the following diagram:

4
/   \
2      6
/ \    / \
1   3  5   7

while the diagrams for the outputs are:

4
/   \
3      6      and    2
/ \           /
5   7         1


Note:

1. The size of the BST will not exceed 50.
2. The BST is always valid and each node’s value is different.

## Solution

The solution to problem 144 to obtain a binary tree’s preorder traversal of its nodes’ values and the solution to problem 1008 to construct a binary search tree given preorder traversal can be used in this solution.

First, obtain the preorder traversal of the original binary search tree. Next, split the preorder traversal into two lists using V. The first list only contains values less than or equal to V. The second list only contains values greater than V. The order of the preorder traversal is remained in both lists.

Then, use the two lists as preorder traversals of the two subtrees, and construct two subtrees which are binary search trees. Since it is required that most of the structure of the original tree should remain, the two subtrees must be binary search trees.

Finally, return the roots of the two subtrees.

• /**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode[] splitBST(TreeNode root, int V) {
List<Integer> preorderTraversal = preorderTraversal(root);
List<Integer> preorderTraversal0 = new ArrayList<Integer>();
List<Integer> preorderTraversal1 = new ArrayList<Integer>();
int size = preorderTraversal.size();
for (int i = 0; i < size; i++) {
int value = preorderTraversal.get(i);
if (value <= V)
else
}
int length0 = preorderTraversal0.size();
int[] preorder0 = new int[length0];
for (int i = 0; i < length0; i++)
preorder0[i] = preorderTraversal0.get(i);
int length1 = preorderTraversal1.size();
int[] preorder1 = new int[length1];
for (int i = 0; i < length1; i++)
preorder1[i] = preorderTraversal1.get(i);
int[] inorder0 = new int[length0];
System.arraycopy(preorder0, 0, inorder0, 0, length0);
Arrays.sort(inorder0);
int[] inorder1 = new int[length1];
System.arraycopy(preorder1, 0, inorder1, 0, length1);
Arrays.sort(inorder1);
TreeNode[] newBST = new TreeNode;
newBST = buildTree(preorder0, inorder0);
newBST = buildTree(preorder1, inorder1);
return newBST;
}

public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> preorderTraversal = new ArrayList<Integer>();
if (root == null)
return preorderTraversal;
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
TreeNode left = node.left, right = node.right;
if (right != null)
stack.push(right);
if (left != null)
stack.push(left);
}
return preorderTraversal;
}

public TreeNode buildTree(int[] preorder, int[] inorder) {
if (preorder == null || preorder.length == 0)
return null;
TreeNode root = new TreeNode(preorder);
int length = preorder.length;
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
int inorderIndex = 0;
for (int i = 1; i < length; i++) {
int preorderVal = preorder[i];
TreeNode node = stack.peek();
if (node.val != inorder[inorderIndex]) {
node.left = new TreeNode(preorderVal);
stack.push(node.left);
} else {
while (!stack.isEmpty() && stack.peek().val == inorder[inorderIndex]) {
node = stack.pop();
inorderIndex++;
}
node.right = new TreeNode(preorderVal);
stack.push(node.right);
}
}
return root;
}
}

• Todo

• # Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
def splitBST(
self, root: Optional[TreeNode], target: int
) -> List[Optional[TreeNode]]:
def dfs(root):
if root is None:
return [None, None]
if root.val <= target:
l, r = dfs(root.right)
root.right = l
return [root, r]
else:
l, r = dfs(root.left)
root.left = r
return [l, root]

return dfs(root)