Split BST
Problem Descriptionβ
Visit LeetCode for the full problem description.
Solutionsβ
Solution 1: C# (Best: 128 ms)β
| Metric | Value |
|---|---|
| Runtime | 128 ms |
| Memory | 40.7 MB |
| Date | 2021-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β
| Approach | Time | Space |
|---|---|---|
| Solution | To be analyzed | To be analyzed |