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.

递归,操作二叉搜索树

/**
* 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的更多相关文章

  1. 776. Split BST 按大小拆分二叉树

    [抄题]: Given a Binary Search Tree (BST) with root node root, and a target value V, split the tree int ...

  2. [LeetCode] 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 BST

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

  4. 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 ...

  5. #Leetcode# 725. Split Linked List in Parts

    https://leetcode.com/problems/split-linked-list-in-parts/ Given a (singly) linked list with head nod ...

  6. LeetCode 333. Largest BST Subtree

    原题链接在这里:https://leetcode.com/problems/largest-bst-subtree/ 题目: Given a binary tree, find the largest ...

  7. 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 ...

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

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

  9. [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 ...

随机推荐

  1. YUI 阻止动态css加载

    skinnable动态加载 在YUI Module中,经常采用skinnable参数来动态加载css,如: YUI().use('w-paginator', function(Y) { }, requ ...

  2. 修改nopCommerce中的实体

                               对已有实体增加一个属性(对Category增加一个SomeNewProperty)   最近在研究nopcommerce,这里是对官网上文档的学习 ...

  3. Linux chmod命令及权限含义

    对于一条权限赋值命令:sudo chmod  -R 764 ×××:我们必须了解其含义: (1)各字段含义:sudo chmod  -R(更改文件夹及其子文件夹)                    ...

  4. Uva10129 - Play on Words 欧拉通路 DFS

    题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105& ...

  5. 详解python中的__init__与__new__方法

    一.__init__和__new__方法执行的顺序? 在面向对象中介绍了关于对象创建的过程,我们知道__new__方法先于__init__方法执行. 二.__new__方法是什么? 首先,我们先来看下 ...

  6. Python装饰器小代码

    # coding=utf-8import timedef outer(fun): def inner(): start = time.time() fun() runtime = time.time( ...

  7. openvpn服务端与客户端网段互通

    http://www.softown.cn/post/140.html OpenVPN安装.配置教程 http://www.softown.cn/post/137.html openvpn的serve ...

  8. nagios的安装

    Nagios通常由一个主程序(Nagios).一个插件程序(Nagios-plugins)和四个可选的ADDON(NRPE.NSCA. NSClient++和NDOUtils)组成.Nagios的监控 ...

  9. 基于 xorm 的服务端框架 XGoServer

    作者:林冠宏 / 指尖下的幽灵 掘金:https://juejin.im/user/587f0dfe128fe100570ce2d8 博客:http://www.cnblogs.com/linguan ...

  10. SpringMVC 参数绑定注解解析

    本文介绍了用于参数绑定的相关注解. 绑定:将请求中的字段按照名字匹配的原则填入模型对象. SpringMVC就跟Struts2一样,通过拦截器进行参数匹配. 代码在 https://github.co ...