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[0], and output[1] 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.
 

Recursion:

The hard part is how to find recurion logic

Let res[0] be tree less than or equal to V, res[1] be tree greater than V.

Detailed explanation: First of all, we can see that the given root is always there in the answer (either in the bigger subtree or in the smaller subtree). After that, if root->val > V, there is a chance that there is some subtree within the subtree root->left which maybe greater than V and that subtree needs to be attached to root->left. Now, we see that this problem of spliting the "subtree in root->left using V" is the same as the current problem of splitting root. So we can recurse on left and get the required results. One thing to worry about is, what if there is no subtree in root->left that is greater than V? This case is handled automatically by the base case.
Similar argument applies for the case root->val <= V.

 /**
* 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) {
// res[0] is the root of tree <= V, and vise versa for res[1]
TreeNode[] res = new TreeNode[2];
if (root == null) return res; if (root.val > V) {
res[1] = root;
TreeNode[] leftSide = splitBST(root.left, V);
root.left = leftSide[1];
res[0] = leftSide[0];
}
else { // root.val <= V
res[0] = root;
TreeNode[] rightSide = splitBST(root.right, V);
root.right = rightSide[0];
res[1] = rightSide[1];
} return res;
}
}

Leetcode: Split BST的更多相关文章

  1. [LeetCode] Split BST 分割二叉搜索树

    Given a Binary Search Tree (BST) with root node root, and a target value V, split the tree into two ...

  2. LeetCode - 776. Split BST

    Given a Binary Search Tree (BST) with root node root, and a target value V, split the tree into two ...

  3. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  4. [LeetCode] Largest BST Subtree 最大的二分搜索子树

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  5. Leetcode: Largest BST Subtree

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  6. Leetcode: Split Array Largest Sum

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  7. [LeetCode] Split Linked List in Parts 拆分链表成部分

    Given a (singly) linked list with head node root, write a function to split the linked list into k c ...

  8. [LeetCode] Split Array into Consecutive Subsequences 将数组分割成连续子序列

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  9. [LeetCode] Convert BST to Greater Tree 将二叉搜索树BST转为较大树

    Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...

随机推荐

  1. Python学习日记(二十六) 封装和几个装饰器函数

    封装 广义上的封装,它其实是一种面向对象的思想,它能够保护代码;狭义上的封装是面向对象三大特性之一,能把属性和方法都藏起来不让人看见 私有属性 私有属性表示方式即在一个属性名前加上两个双下划线 cla ...

  2. Guava Cache 工具类

    maven依赖 <dependency> <groupId>com.google.guava</groupId> <artifactId>guava&l ...

  3. 类X509Certificate2

    X509Certificate2类的初始化. 文章:使用X509Certificate2类操作证书文件 参考:https://docs.microsoft.com/zh-cn/dotnet/api/s ...

  4. class Pagination(object)分页源码

    class Pagination(object): def init(self, current_page, all_count, per_page_num=10, pager_count=11): ...

  5. java 获取对象的数据类型

    // java 获取对象的数据类型 public static String getType(Object object){ String typeName=object.getClass().get ...

  6. node爬虫爬取中文时乱码问题 | nodejs gb2312、GBK中文乱码解决方法

    iconv需要依赖native库,这样一来,在一些不支持native模块安装的虚拟主机和windows平台上,我们还是无法安心处理GBK编码. 老外写了一个通过纯Javascript转换编码的模块 i ...

  7. vue $emit、$on、$refs简介

    1.$emit 触发当前实例上的事件.附加参数都会传给监听器回调 ex: 子组件调用父组件的方法并传递数据注意:子组件标签中的时间也不区分大小写要用“-”隔开 子组件: <template> ...

  8. MySQL 硬链接删除大表

    在清理整个大表时,我们推荐使用drop,而非delete.但是如果表实在太大,即使是drop,也需要消耗一定的时间.这时可以利用linux的硬连接来快速删除大表,操作过程如下:有一个大表test,共有 ...

  9. 虚拟机Linux系统ip查询失败问题

    当用SSH连接Linux需要ip地址,但是不论是通过ipconfig命令,还是通过ip addr命令都无法获取Linux的ip,通过以下方法成功解决了该问题: 1.点击编辑里面的虚拟网络编辑器出现如下 ...

  10. 完美兼容IE10以下所有版本

    IE一直是个恶心东西 各种不支持 现在发现了个好东西可以兼容ie10以下所有浏览器 <!--[if lte IE 9]><script>window.location.href ...