LeetCode - 776. 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.
递归,操作二叉搜索树
/**
* 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) {
if (root == null)
return new TreeNode[]{null, null};
if (root.val == V) {
TreeNode right = root.right;
root.right = null;
return new TreeNode[]{root, right};
}
else if (root.val > V) {
TreeNode[] nodes = splitBST(root.left, V);
TreeNode left = nodes[0];
TreeNode right = nodes[1];
root.left = right;
return new TreeNode[]{left,root};
} else {
TreeNode[] nodes = splitBST(root.right, V);
TreeNode left = nodes[0];
TreeNode right = nodes[1];
root.right=left;
return new TreeNode[]{root, right};
} }
}
LeetCode - 776. Split BST的更多相关文章
- 776. Split BST 按大小拆分二叉树
[抄题]: Given a Binary Search Tree (BST) with root node root, and a target value V, split the tree int ...
- [LeetCode] Split BST 分割二叉搜索树
Given a Binary Search Tree (BST) with root node root, and a target value V, split the tree into two ...
- Leetcode: Split BST
Given a Binary Search Tree (BST) with root node root, and a target value V, split the tree into two ...
- LeetCode 538. Convert BST to Greater Tree (把二叉搜索树转换成较大树)
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...
- #Leetcode# 725. Split Linked List in Parts
https://leetcode.com/problems/split-linked-list-in-parts/ Given a (singly) linked list with head nod ...
- LeetCode 333. Largest BST Subtree
原题链接在这里:https://leetcode.com/problems/largest-bst-subtree/ 题目: Given a binary tree, find the largest ...
- LeetCode 725. 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] 659. Split Array into Consecutive Subsequences 将数组分割成连续子序列
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
- [LeetCode] 410. Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
随机推荐
- Handler processing failed; nested exception is java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config解决
出现这个问题往往伴随 HTTP-500错误 报错信息: HTTP Status - Handler processing failed; nested exception is java.lang. ...
- Vuethink正确安装过程
1. 下载项目vuethink,本例将项目放置website文件下. 2. 后台搭建 本地建站–>以phpstudy为例 1) 新建站点域名 <Virtual ...
- TCP三次握手机制中的seq和ack
TCP连接的三次握手:第一次(A--->B),SYN=1,seq=x第二次(B--->A),SYN=1,ACK=1,seq=y,ack=x+1 第三次(A--->B),ACK=1,s ...
- Oracle积累
Oracle积累 1 用To_char()转换带小数的数值. 例如:0.125 取两位小数. To_char(0.125,'FM990.99') -> 0.13 注: 格 ...
- ThinkServer TD340服务器安装操作系统[转]
一:服务器概况 服务器是联想旗下ThinkServer品牌TD340型号服务器,服务器标配32G内存,1T硬盘.其中服务器使用RAID(磁盘阵列)技术,拥有一个RAID卡,服务器标配一个大小为1T的磁 ...
- linux_nginx_rewrite
什么是Nginx的rewrite? 实现URL地址重写,比较复杂的write需要开发来完成,伪静态处理实现是开发的工作, 这rewrite写在location中 指令语法: rewrite re ...
- Linux指令--grep
原文地址:http://www.cnblogs.com/peida/archive/2012/12/17/2821195.html.感谢作者的无私分享. Linux系统中grep命令是一种强大的文本搜 ...
- Linux指令--ping
Linux系统的ping命令是常用的网络命令,它通常用来测试与目标主机的连通性,我们经常会说"ping一下某机器,看是不是开着".不能打开网页时会说"你先ping网关地址 ...
- 腾讯工程师教你玩转 RocksDB
欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 作者:腾讯云数据库内核团队 原文标题:[腾讯云CDB]教你玩转MyRocks/RocksDB-STATISTICS与后台线程篇 0. Intro ...
- java 包命名规范
转载 原文地址:http://blog.csdn.net/kongjiea/article/details/45365753 Java的包名都有小写单词组成,类名首字母大写:包的路径符合所开发的 系统 ...