Leetcode: Split BST
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:
- The size of the BST will not exceed
50
. - 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的更多相关文章
- [LeetCode] Split BST 分割二叉搜索树
Given a Binary Search Tree (BST) with root node root, and a target value V, split the tree into two ...
- LeetCode - 776. Split BST
Given a Binary Search Tree (BST) with root node root, and a target value V, split the tree into two ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [LeetCode] Largest BST Subtree 最大的二分搜索子树
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- Leetcode: Largest BST Subtree
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- Leetcode: Split Array Largest Sum
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [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 ...
- [LeetCode] Split Array into Consecutive Subsequences 将数组分割成连续子序列
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
- [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 ...
随机推荐
- Linux环境宿主机进入Docker容器、连接数据库、复制文件
我们默认mysql容器已经正常启动,以下为关键命令.1.docker exec -it mysql bash : 进入已经正常启动的容器bash中,mysql是指实际容器名称.2.mysql -uro ...
- Docker Private Registry 常用组件
Docker Private Registry 常用组件 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Docker Registry概述 1>.什么是registry ...
- Linux 设置系统编码
1.locale -a查看系统支持的语言2.进入etc/sysconfig/3.编辑i18n4.修改lang 5.设置完成后刷新:i18n source /etc/sysconfig/i18n
- 使用Xpath+多进程爬取诗词名句网的史书典籍类所有文章。update~
上次写了爬取这个网站的程序,有一些地方不完善,而且爬取速度较慢,今天完善一下并开启多进程爬取,速度就像坐火箭.. # 需要的库 from lxml import etree import reques ...
- windows下面,PHP如何启动一些扩展功能
我今天在试这个时,发现php有些默认设置,是需要人为介入修改的. 比如,当我们在安装一个软件,而这个软件需要启用php一些扩展功能. 那么,按一般套路,将php.ini文件里的相关行的注释去掉即可. ...
- python学习类与方法的调用规则
1类方法的特点是类方法不属于任何该类的对象,只属于类本身 2类的静态方法类似于全局函数,因为静态方法既没有实例方法的self参数也没有类方法的cls参数,谁都可以调用 3.实例方法只属于实例,是实例化 ...
- 基于源代码为树莓派设备构建 TensorFlow
本指南为运行 Raspbian 9.0 操作系统的 Raspberry Pi 嵌入式设备构建 TensorFlow.虽然这些说明可能也适用于其他系列的 Raspberry Pi 设备,但它仅针对此文中 ...
- rocketmq那些事儿之本地调试环境搭建
上一篇文章中我们已经介绍过rocketmq的集群环境搭建,然而在源码的学习中我们还需要进行本地的调试和问题的定位查找,毕竟还是在本地方便些,今天就说一说如何进行源码的本地调试 下载编译 对于rocke ...
- centos7部署etcd集群
实验环境:centos7.4纯净版 192.168.216.130 node1 master 192.168.216.132 node2 slave 192.168.216.134 node3 sla ...
- Pycharm中打开Terminal方式
点击剪头的图标就可以在左侧出现Terminal