Skip to main content

Split BST

LeetCode Link

Problem Description​

Visit LeetCode for the full problem description.


Solutions​

Solution 1: C# (Best: 128 ms)​

MetricValue
Runtime128 ms
Memory40.7 MB
Date2021-11-23
Solution
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public TreeNode[] SplitBST(TreeNode root, int target) {
if (root == null)
{
return new TreeNode[] { null, null };
}

if(root.val <= target)
{
TreeNode[] splitted = SplitBST(root.right, target);
root.right = splitted[0];
return new TreeNode[] {root, splitted[1]};
}
else
{
TreeNode[] splitted = SplitBST(root.left, target);
root.left = splitted[1];
return new TreeNode[] {splitted[0], root};
}
}
}

Complexity Analysis​

ApproachTimeSpace
SolutionTo be analyzedTo be analyzed