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 ...
随机推荐
- Java 设计模式之抽象工厂模式(三)
原文地址:Java 设计模式之抽象工厂模式(三) 博客地址:http://www.extlight.com 一.前言 上篇文章 <Java 设计模式之工厂模式(二)>,介绍了简单工厂模式和 ...
- golang的slice作为函数参数传值的坑
直接贴代码 func sliceModify(slice []int) { // slice[0] = 88 slice = append(slice, ) } func main() { slice ...
- 一些非常好的VC++/MFC开源项目链接
Introduction List of some of the best Open Source projects written in VC++/MFC. Background Codeproje ...
- 如何使用indexdb
一.实现步骤 1)获得indexedDB对象 if (!window.indexedDB) { window.indexedDB = window.mozIndexedDB || window.web ...
- mac 电脑下添加 HTMLtestrunner.py 生成 报表
HTMLTestRunner是Python标准库unittest模块的一个扩展.它生成易于使用的HTML测试报告. 1.下载HTMLTestRunner.py模块地址 http://tungwaiyi ...
- Java中的<< 和 >> 和 >>> 详细分析
<<表示左移移,不分正负数,低位补0: 注:以下数据类型默认为byte-8位 左移时不管正负,低位补0 正数:r = 20 << 2 20的二进制补码:0001 0100 向左 ...
- bytes数据类型和字符串的编码解码,三元运算,进制互换
三元运算 如果这个条件成立就存这个值,如果那个条件成立就存那个值. 进制 bytes类型,字节数据类型也就是二进制类型,这个是python3专有数据类型,在python2里跟字符串是一个类型,也就是p ...
- python 源代码保护 之 xx.py -> xx.so
前情提要 之前由于项目的需要,需要我们将一部分“关键代码”隐藏起来. 虽然Python 先天支持 将源代码 编译后 生成 xxx.pyc 文件,但是破解起来相当容易 -_-!! 于是搜罗到了另外一种方 ...
- 浅谈PHP面向对象编程(八、多态)
8.0 多态 在设计一个成员方法时,通常希望该方法具备一定的通用性.例如要实现一个动物叫的方法,由于每个动物的叫声是不同的,因此可以在方法中接收-个动物类型的参数的对象当传人猫类对象时就发出猫类的叫 ...
- 一行命令解决 xcode升级新版本插件失效问题
sudo find ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins -name Info.plist -maxdepth ...