[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道怎么返回2个二叉树:用TreeNode[]数组,分为0 1两个变量即可

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. traverse获得的是一个数组,然后splitted[0]一个点可以给tree中的一个点

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

返回2个二叉树:用TreeNode[]数组,分为0 1两个变量即可

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

/**
* 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) {
//initialization
TreeNode[] splitted = new TreeNode[2]; //corner case
if (root == null) return splitted; //discuss 2 cases
if (V >= root.val) {
splitted = splitBST(root.right, V);
root.right = splitted[0];
splitted[0] = root;
}else {
splitted = splitBST(root.left, V);
root.left = splitted[1];
splitted[1] = root;
} //return
return splitted;
}
}

776. Split BST 按大小拆分二叉树的更多相关文章

  1. LeetCode - 776. Split BST

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

  2. 算法进阶面试题05——树形dp解决步骤、返回最大搜索二叉子树的大小、二叉树最远两节点的距离、晚会最大活跃度、手撕缓存结构LRU

    接着第四课的内容,加入部分第五课的内容,主要介绍树形dp和LRU 第一题: 给定一棵二叉树的头节点head,请返回最大搜索二叉子树的大小 二叉树的套路 统一处理逻辑:假设以每个节点为头的这棵树,他的最 ...

  3. 【SQL】sql版Split函数。用于拆分字符串为单列表格

    功能与.net版string.Split函数类似,只不过.net返回的是数组,这个返回的是一个单列表格,每个拆分出来的子串占一行.可选是否移除空格子串和重复项.市面上类似的函数不算少,但大多都是在循环 ...

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

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

  5. 将一个byte[]数组根据大小拆分为若干小byte[]数组方法

    /// <summary> /// 将大数组拆分为多个小数组 /// </summary> /// <param name="superbyte"&g ...

  6. Leetcode: Split BST

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

  7. log4j 日志限制大小 拆分成30个 不按日期分日志 按大小拆分 按日期产生

    先说一下按日期产生,不解释,大家都懂,这种方法的缺点就是很吃硬盘空间 log4j.rootLogger=INFO,logfile,stdout log4j.logger.java.sql=DEBUG, ...

  8. 算法与数据结构基础 - 二叉查找树(Binary Search Tree)

    二叉查找树基础 二叉查找树(BST)满足这样的性质,或是一颗空树:或左子树节点值小于根节点值.右子树节点值大于根节点值,左右子树也分别满足这个性质. 利用这个性质,可以迭代(iterative)或递归 ...

  9. 算法与数据结构基础 - 递归(Recursion)

    递归基础 递归(Recursion)是常见常用的算法,是DFS.分治法.回溯.二叉树遍历等方法的基础,典型的应用递归的问题有求阶乘.汉诺塔.斐波那契数列等,可视化过程. 应用递归算法一般分三步,一是定 ...

随机推荐

  1. Pycharm自动添加文件头

    Pycharm自动添加文件头 在编程的时候,我们往往需要在文件头里添加一些编码和作者信息,在Pycharm中,系统给我们自带了这一功能,可以做如下设置: 打开设置 在设置中找到如下选项: 然后在编辑框 ...

  2. 【转】使用VisualStudio完成自动化C++代码生成和编译工作(GacUI)

    使用VisualStudio完成自动化C++代码生成和编译工作(GacUI)     GacUI终于进入制作dll的阶段了.昨天上传了一个新的工程,在Vczh Library++3.0(E:\Code ...

  3. debian下arp欺骗

    sudo sysctl -w net.ipv4.ip_forward= sudo sysctl -p arpspoof -i eth0 -t 目标ip -r 伪装ip或者ettercap -i eth ...

  4. 轻量应用服务器安装 phpMyAdmin

    第一步:在phpMyAdmin官方网站http://www.phpmyadmin.net/downloads/下载源码包并解压 cd /usr/local/src wget https://files ...

  5. tomcat窗口一闪而过

    当点击bin/startup.bat,出现黑窗口一闪而过时,肯定是因为tomcat启动报错了. 错误排查方法 首先检查java环境变量是否设置正确. 其次调试tomcat,需要修改startup.ba ...

  6. python开发购物车

    1 业务需求 商品中心 显示库存的商品 商品能够加入到购物车 个人中心 购物车 修改购物车的商品 下单 完成的订单 订单详情 账户余额 2 代码实现 # 定义全局变量信息 # 商品编号信息 goods ...

  7. crossdomain.xml配置不当的利用和解决办法

    00x1: 今天在无聊的日站中发现了一个flash小站,点进crossdomain.xml一看,震惊 本屌看到这个*就发觉事情不对 百度一下,这是一个老洞,配置不当能引起各种问题就算能远程加载恶意的s ...

  8. 涂抹mysql笔记-管理mysql服务

    -DSYSCONFDIR=/mysql/conf \ 所以在conf下建立my.cnf文件 vi my.cnf [client]port=3306socket=/mysql/conf/mysql.so ...

  9. rest api方式实现对文档库的管理

    写在前面 刚入职一家新公司,在对接app的时候需要获取到某公司的sharepoint上面的文档库,获取文档库列表,团队文档库中的文件和文件夹列表,个人文档库中的文件文件夹列表,及在app端进入文件夹的 ...

  10. Django model 字段类型及选项解析

    字段类型选择: AutoField(Field) - int自增列,必须填入参数 primary_key=True BigAutoField(AutoField) - bigint自增列,必须填入参数 ...