[抄题]:

Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary search tree.

Example 1:

  1. Input:
  2. 1
  3. / \
  4. 0 2
  5.  
  6. L = 1
  7. R = 2
  8.  
  9. Output:
  10. 1
  11. \
  12. 2

Example 2:

  1. Input:
  2. 3
  3. / \
  4. 0 4
  5. \
  6. 2
  7. /
  8. 1
  9.  
  10. L = 1
  11. R = 3
  12.  
  13. Output:
  14. 3
  15. /
  16. 2
  17. /
  18. 1

[暴力解法]:

时间分析:

空间分析:

[奇葩输出条件]:

判断节点的值是否不在范围内,而非节点是否非空。第一次见,要注意。

[奇葩corner case]:

[思维问题]:

[一句话思路]:

分左右之后为了保持一路的继承关系,还是左节点traverse左节点

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

[画图]:

[一刷]:

  1. traverse中的主操作是对root一个节点进行的,操作完直接返回。分左右之后为了保持一路的继承关系,还是左节点traverse左节点,操作完还有下一步,不用返回。

[二刷]:

[三刷]:

[四刷]:

[五刷]:

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

[总结]:

traverse中的主操作是对root一个节点进行的,操作完直接返回。

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

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

左右是节点,因为左右子树都是新建的

[关键模板化代码]:

  1. //trim left
  2. root.left = trimBST(root.left, L, R);
  3. //trim right
  4. root.right = trimBST(root.right, L, R);

[其他解法]:

[Follow Up]:

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

[代码风格] :

  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 trimBST(TreeNode root, int L, int R) {
  12. //corner case: if root is null
  13. if (root == null) {
  14. return null;
  15. }
  16. //corner case: if left or right is out of bound
  17. if (root.val < L) {
  18. return trimBST(root.right, L, R);
  19. }
  20. if (root.val > R) {
  21. return trimBST(root.left, L, R);
  22. }
  23. //trim left
  24. root.left = trimBST(root.left, L, R);
  25. //trim right
  26. root.right = trimBST(root.right, L, R);
  27. //return
  28. return root;
  29. }
  30. }

669. Trim a Binary Search Tree修剪二叉搜索树的更多相关文章

  1. LeetCode 669. Trim a Binary Search Tree修剪二叉搜索树 (C++)

    题目: Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so th ...

  2. [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树

    4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...

  3. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  4. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  5. [LeetCode] Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  6. [LeetCode] Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  7. LeetCode 235. Lowest Common Ancestor of a Binary Search Tree (二叉搜索树最近的共同祖先)

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  8. [leetcode]99. Recover Binary Search Tree恢复二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  9. [Leetcode] Recover binary search tree 恢复二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

随机推荐

  1. Request.UrlReferrer详解

    使用前需要进行判断: if (Request != null && Request.UrlReferrer != null && Request.UrlReferrer ...

  2. 搭建基于hyperledger fabric的联盟社区(四) --chaincode开发

    前几章已经分别把三台虚拟机环境和配置文件准备好了,在启动fabric网络之前我们要准备好写好的chaincode.chaincode的开发一般是使用GO或者JAVA,而我选择的是GO语言.先分析一下官 ...

  3. C++中如何在顺序容器中删除符合特定条件的元素

    以前很少做删除操作,vector一直当成数组用,而实际追求效率时又经常舍弃vector选用C风格数组.看<C++ Primer>到顺序容器删除这节时试着实现课后习题结果一动手我就出错了. ...

  4. Java基础--NIO

    NIO库在JDK1.4中引入,它以标准Java代码提供了高速的,面向块的IO,弥补了之前同步IO的不足. 缓冲区Buffer Buffers是一个对象,包含了一些要写入或读出的数据.在面向流的IO模型 ...

  5. 支付宝吱口令自动复制脚本,自动复制 JavaScript 代码介绍

    本文转自:http://www.sojson.com/blog/262.html 最近支付宝#吱口令#的信息随处可见,可谓是铺天盖地,群里发这样的信息给被踢了不少.我开始还在鄙视这些人,有几个小钱?然 ...

  6. python的可变数据类型和不可变类型

    python里面一切皆对象 ython的每个对象都分为可变类型和不可变类型 整形,浮点型,字符串,元组属于不可变类型,列表,字典是可变类型 不可变数据类型 对不可变类型的变量重新赋值,实际上是重新创建 ...

  7. hotplug_uevent机制_修改mdev配置支持U盘自动挂载学习笔记

    1.接入U盘,看输出打印信息并分析 (1)输出信息 自动创建设备节点 (2)用ls命令查看 这里/dev/sda表示整个U盘,/dev/sda1表示这个U盘的第一个分区. (3)手动挂载,查看文件,手 ...

  8. 浅谈PHP面向对象编程(二、基础知识)

    和一些面向对象的语言有所不同,PHP并不是一种纯面向对象的语言,包PIP它支持面向对象的程序设计,并可以用于开发大型的商业程序.因此学好面向对象输程对PHP程序员来说也是至关重要的.本章并针对面向对象 ...

  9. 线程queue、线程进程池、异步回调机制

    1. 线程 queue queue is especially useful in threaded programming when information must be exchanged sa ...

  10. Pymol

    如何用Pymol做出那些美呆的结构图(基础篇) 2016-10-31  翾园  摘自 BioEngX生化...  阅 1079  转 6 转藏到我的图书馆   微信分享:   摘自微信公众号:BioE ...