[抄题]:

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:

  1. Input: root = [4,2,6,1,3,5,7], V = 2
  2. Output: [[2,1],[4,3,6,null,null,5,7]]
  3. Explanation:
  4. Note that root, output[0], and output[1] are TreeNode objects, not arrays.
  5.  
  6. The given tree [4,2,6,1,3,5,7] is represented by the following diagram:
  7.  
  8. 4
  9. / \
  10. 2 6
  11. / \ / \
  12. 1 3 5 7
  13.  
  14. while the diagrams for the outputs are:
  15.  
  16. 4
  17. / \
  18. 3 6 and 2
  19. / \ /
  20. 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的代码] :

[潜台词] :

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public TreeNode[] splitBST(TreeNode root, int V) {
  12. //initialization
  13. TreeNode[] splitted = new TreeNode[2];
  14.  
  15. //corner case
  16. if (root == null) return splitted;
  17.  
  18. //discuss 2 cases
  19. if (V >= root.val) {
  20. splitted = splitBST(root.right, V);
  21. root.right = splitted[0];
  22. splitted[0] = root;
  23. }else {
  24. splitted = splitBST(root.left, V);
  25. root.left = splitted[1];
  26. splitted[1] = root;
  27. }
  28.  
  29. //return
  30. return splitted;
  31. }
  32. }

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. ios-UILabel居中随内容自适应,后面的控件跟在其后

    如图绿蓝框所示,UILabel显示名字,Label框随名字长短而自适应,后面的性别图片跟在其后显示 分两部分:第一部分先布局 //名字 self.nameLab = [[UILabel alloc]i ...

  2. golang-generate-1pixel-image

    package main import ( "bytes" "encoding/base64" "flag" "html/temp ...

  3. Redis 集群知识点及命令

    Redis 集群命令 备注 cluster nodes 查看集群包含的节点 cluster meet <ip> <port> 将 ip 和 port 所指定的节点添加到 nod ...

  4. Linux之cd、pwd、mkdir、rmdir

    cd.pwd.mkdir.rmdir 命令功能: 切换到指定的目录,可用绝对路径和相对路径 命令格式: cd directory 命令参数: 无 命令实例: 1.切换到/bin目录 vbird@Ubu ...

  5. 第二次作业——分布式版本控制系统Git的安装与使用

    作业要求来自:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2097 远程仓库地址是:https://github.com/sheep ...

  6. Scrapy学习篇(十一)之设置随机User-Agent

    大多数情况下,网站都会根据我们的请求头信息来区分你是不是一个爬虫程序,如果一旦识别出这是一个爬虫程序,很容易就会拒绝我们的请求,因此我们需要给我们的爬虫手动添加请求头信息,来模拟浏览器的行为,但是当我 ...

  7. Ali流量控制中间件Sentinel

    原文链接: https://blog.csdn.net/u012190514/article/details/81383698 Sentinel 是什么 随着微服务的流行,服务和服务之间的稳定性变得越 ...

  8. vmware中的linux虚拟机配置以nat模式上网,并用xshell连接该虚拟机

    1.  首先确保宿主机上的vmnet8处于启用状态 2.  以管理员身份运行vmware >> 编辑 >> 虚拟机网络编辑器 >> 选中Vmnet8 >> ...

  9. 02-模拟Junit4功能

    package com.day2; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; im ...

  10. [HNOI2012]射箭(计算几何)

    设抛物线方程\(y = ax^2 + bx\), 那么对于一个靶子\((x_i,y_{down},y_{up})\)我们需要满足的条件就是 \(\frac{y_{down}}{x_i} \leq ax ...