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 ...
随机推荐
- wordpress域名解析到了网站,但是点击其他页面会出现ip而不是域名
1.前提域名可以访问你的网站证明解析没问题 2.那就是wp后台的设置问题,将url和站点url改为你的域名http://www.eovision.cc清理缓存即可 亲测可用,如果改了出现页面 ...
- MEF学习总结(2)---Primitive层
Primitive层是属于依赖注入的通用模型,主要有如下核心类型: 1. ComposablePart是核心类,他表示组件容器中的每一个组件,是对真正组件实例的包装.ExportDefinition属 ...
- 编译Lichee(FridenlyARM NanoPi-M1)碰到的问题及解决办法
1. 提示libz.so.1找不到 需要在ubuntu上安装下面两个包: sudo apt-get install lib32ncurses5 ia32-libs 2. 提示xt_hl.o没有make ...
- 从C++Primer某习题出发,谈谈C语言标准I/O的缓存问题
刚看完信号那章,觉得处理信号时的sigsetjmp/siglongjmp似乎跟异常的跳出很像,于是想去复习C++异常,然后发现了对I/O没有充分理解的问题. 题目是C++ Primer 5.6.3节的 ...
- C++中const使用注意要点(二)
当const修饰类的成员变量 1.const修饰类的非静态成员时必须在构造函数初始化列表上初始化: 在构造函数内会提示表达式必须是可修改的左值,因为在构造函数内并不是初始化,仅仅是赋值,而const类 ...
- Required String parameter 'id' is not present
问题详情: 简单的说,我就是通过ajax发起了一个post请求到后台,但是后台没有收到请求发过去的参数,并且还报了这样的错误. 错误描述告诉我们,请求参数里面并没有存在id.我 ...
- node中express的中间件之basicAuth
basicAuth中间件为网站添加身份认证功能.在使用了该中间件后, 用户访问网站时必须输入用户名与密码,在用户输入了用户名与密码并通过验证之后才能访问网站. 当用户输入的用户名和密码符合条件,中间件 ...
- Python - Pyinstaller
安装 Pyinstaller pip install pyinstaller 使用: test.py print("Hello World!") 命令行输入 pyinstaller ...
- Django的contenttypes应用、缓存相关
一.django的contenttypes contenttypes 是Django内置的一个应用 , 可以追踪项目中所有app 和 model 的对应关系, 并记录djang_content_typ ...
- ARM汇编 均值滤波实验
实验要求是排序后去掉最大值最小值,然后把剩下的求平均数. 排序可以用之前的冒泡排序,关键的问题是求平均数.因为ARM没有除法,应该怎么求平均数呢? 最简单的方法就是减法了,用被除数一直减除数,看减了多 ...