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 ...
随机推荐
- Tree Recovery(由先、中序列构建二叉树)
题目来源: http://poj.org/problem?id=2255 题目描述: Description Little Valentine liked playing with binary tr ...
- Web Component总结
Web Component 一个Web组件通常由四个部分组成:模板.Shadow DOM.自定义元素与打包,其中Shadow DOM解决了组件在页面中的封装问题 Shadow DOM 有shadow ...
- DEDE在图集列表中调出图集的所有图片[首页也适用]
在include/common.func.php 中添加以下函数代码 代码如下: // 在图集列表中调出图集的所有图片 function Getimgs($aid, $imgwith = 220, ...
- 求助帖:android开发初期:为什么我在活动二设置的singInstance模式跑到活动三去了???
求android开发的高手帮我看看这个问题吧: <activity android:name=".SecondActivity" android:label="Th ...
- 数据库复习总结(2)-SQLServer的管理
1.需要安装SQLServer2008或者SQLServer2012,若要使用SQLServer管理工具进行开发还要安装SQL Server Management Studio,还可以使用Visual ...
- 【问题解决】Eclipse中 ctrl+空格 content assist
改一下你的快捷键设置:window->perferences-->keys--->查找 content assist--->把这个地方改成你想要的就可以了.!
- angular4 中自定义pagination组件
你用Angular 吗? 一.介绍 一个基于angular4 开发的可以分页的组件.组件的好处就是可以复用,复用.....作为一个前端码农,开始的分页功能实现是我用jquery写的,其他同事用的时候都 ...
- spring问题:java.lang.NoClassDefFoundError: org/aspectj/weaver/tools/PointcutPrimitive
问题描述: spring的:java.lang.NoClassDefFoundError: org/aspectj/weaver/tools/PointcutPrimitive 解决方案: 缺少jar ...
- Windows和Linux如何使用Java代码实现关闭进程
在用selenium做自动化测试时,由于各种不明原因,有时Chrome浏览器会出现假死的情况,也就是整个浏览器响应超时,本人脚本主要部署在Windows机器上,所以主要以Windows为主,浏览器为C ...
- SpringAOP简单入门
注解形式 步骤一.定义一个interface public interface ArithmeticCalculator { double plus(int i, int j); double sub ...