669. Trim a Binary Search Tree修剪二叉搜索树
[抄题]:
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:
- Input:
- 1
- / \
- 0 2
- L = 1
- R = 2
- Output:
- 1
- \
- 2
Example 2:
- Input:
- 3
- / \
- 0 4
- \
- 2
- /
- 1
- L = 1
- R = 3
- Output:
- 3
- /
- 2
- /
- 1
[暴力解法]:
时间分析:
空间分析:
[奇葩输出条件]:
判断节点的值是否不在范围内,而非节点是否非空。第一次见,要注意。
[奇葩corner case]:
[思维问题]:
[一句话思路]:
分左右之后为了保持一路的继承关系,还是左节点traverse左节点
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- traverse中的主操作是对root一个节点进行的,操作完直接返回。分左右之后为了保持一路的继承关系,还是左节点traverse左节点,操作完还有下一步,不用返回。
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
traverse中的主操作是对root一个节点进行的,操作完直接返回。
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
左右是节点,因为左右子树都是新建的
[关键模板化代码]:
- //trim left
- root.left = trimBST(root.left, L, R);
- //trim right
- root.right = trimBST(root.right, L, R);
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode(int x) { val = x; }
- * }
- */
- class Solution {
- public TreeNode trimBST(TreeNode root, int L, int R) {
- //corner case: if root is null
- if (root == null) {
- return null;
- }
- //corner case: if left or right is out of bound
- if (root.val < L) {
- return trimBST(root.right, L, R);
- }
- if (root.val > R) {
- return trimBST(root.left, L, R);
- }
- //trim left
- root.left = trimBST(root.left, L, R);
- //trim right
- root.right = trimBST(root.right, L, R);
- //return
- return root;
- }
- }
669. Trim a Binary Search Tree修剪二叉搜索树的更多相关文章
- 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 ...
- [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 ...
- [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 ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [LeetCode] Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [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 ...
- 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 ...
- [leetcode]99. Recover Binary Search Tree恢复二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [Leetcode] Recover binary search tree 恢复二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
随机推荐
- Request.UrlReferrer详解
使用前需要进行判断: if (Request != null && Request.UrlReferrer != null && Request.UrlReferrer ...
- 搭建基于hyperledger fabric的联盟社区(四) --chaincode开发
前几章已经分别把三台虚拟机环境和配置文件准备好了,在启动fabric网络之前我们要准备好写好的chaincode.chaincode的开发一般是使用GO或者JAVA,而我选择的是GO语言.先分析一下官 ...
- C++中如何在顺序容器中删除符合特定条件的元素
以前很少做删除操作,vector一直当成数组用,而实际追求效率时又经常舍弃vector选用C风格数组.看<C++ Primer>到顺序容器删除这节时试着实现课后习题结果一动手我就出错了. ...
- Java基础--NIO
NIO库在JDK1.4中引入,它以标准Java代码提供了高速的,面向块的IO,弥补了之前同步IO的不足. 缓冲区Buffer Buffers是一个对象,包含了一些要写入或读出的数据.在面向流的IO模型 ...
- 支付宝吱口令自动复制脚本,自动复制 JavaScript 代码介绍
本文转自:http://www.sojson.com/blog/262.html 最近支付宝#吱口令#的信息随处可见,可谓是铺天盖地,群里发这样的信息给被踢了不少.我开始还在鄙视这些人,有几个小钱?然 ...
- python的可变数据类型和不可变类型
python里面一切皆对象 ython的每个对象都分为可变类型和不可变类型 整形,浮点型,字符串,元组属于不可变类型,列表,字典是可变类型 不可变数据类型 对不可变类型的变量重新赋值,实际上是重新创建 ...
- hotplug_uevent机制_修改mdev配置支持U盘自动挂载学习笔记
1.接入U盘,看输出打印信息并分析 (1)输出信息 自动创建设备节点 (2)用ls命令查看 这里/dev/sda表示整个U盘,/dev/sda1表示这个U盘的第一个分区. (3)手动挂载,查看文件,手 ...
- 浅谈PHP面向对象编程(二、基础知识)
和一些面向对象的语言有所不同,PHP并不是一种纯面向对象的语言,包PIP它支持面向对象的程序设计,并可以用于开发大型的商业程序.因此学好面向对象输程对PHP程序员来说也是至关重要的.本章并针对面向对象 ...
- 线程queue、线程进程池、异步回调机制
1. 线程 queue queue is especially useful in threaded programming when information must be exchanged sa ...
- Pymol
如何用Pymol做出那些美呆的结构图(基础篇) 2016-10-31 翾园 摘自 BioEngX生化... 阅 1079 转 6 转藏到我的图书馆 微信分享: 摘自微信公众号:BioE ...