[LeetCode] 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
这道题让我们修剪一棵二叉搜索树,给了个边界范围[L, R], 所有不在这个范围内的结点应该被移除掉,但是仍需要保留二叉搜索树的性质,即左<根<右,有时候是小于等于。博主最开始的想法是先遍历一遍二叉树,将在返回内的结点值都放到一个数组后,遍历结束后再根据数组重建一棵二叉搜索树。这种方法会在某些test case上fail掉,可能会改变原来的二叉搜索树的结构,所以我们只能换一种思路。正确方法其实应该是在遍历的过程中就修改二叉树,移除不合题意的结点。当然对于二叉树的题,十有八九都是要用递归来解的。首先判断如果root为空,那么直接返回空即可。然后就是要看根结点是否在范围内,如果根结点值小于L,那么返回对其右子结点调用递归函数的值;如果根结点大于R,那么返回对其左子结点调用递归函数的值。如果根结点在范围内,将其左子结点更新为对其左子结点调用递归函数的返回值,同样,将其右子结点更新为对其右子结点调用递归函数的返回值。最后返回root即可,参见代码如下:
解法一:
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int L, int R) {
if (!root) return NULL;
if (root->val < L) return trimBST(root->right, L, R);
if (root->val > R) return trimBST(root->left, L, R);
root->left = trimBST(root->left, L, R);
root->right = trimBST(root->right, L, R);
return root;
}
};
下面这种方法是迭代的写法,虽然树的题一般都是用递归来写,简洁又美观。但是我们也可以强行用while来代替递归,比如下面这种写法:
解法二:
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int L, int R) {
if (!root) return NULL;
while (root->val < L || root->val > R) {
root = (root->val < L) ? root->right : root->left;
}
TreeNode *cur = root;
while (cur) {
while (cur->left && cur->left->val < L) {
cur->left = cur->left->right;
}
cur = cur->left;
}
cur = root;
while (cur) {
while (cur->right && cur->right->val > R) {
cur->right = cur->right->left;
}
cur = cur->right;
}
return root;
}
};
参考资料:
https://discuss.leetcode.com/topic/102034/java-solution-6-liner
https://discuss.leetcode.com/topic/104140/java-solution-iteration-version
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Trim a Binary Search Tree 修剪一棵二叉搜索树的更多相关文章
- [LeetCode] 270. Closest Binary Search Tree Value 最近的二叉搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- leetCode 95.Unique Binary Search Trees II (唯一二叉搜索树) 解题思路和方法
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 109 Convert Sorted List to Binary Search Tree 有序链表转换二叉搜索树
给定一个单元链表,元素按升序排序,将其转换为高度平衡的BST.对于这个问题,一个高度平衡的二叉树是指:其中每个节点的两个子树的深度相差不会超过 1 的二叉树.示例:给定的排序链表: [-10, -3, ...
- Leetcode109. Convert Sorted List to Binary Search Tree有序链表转换二叉搜索树
给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定的有序链表: [-10 ...
- 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 ...
- LeetCode - 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 a ...
- 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 ...
- [LeetCode] 272. Closest Binary Search Tree Value II 最近的二分搜索树的值之二
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- LeetCode OJ :Unique Binary Search Trees II(唯一二叉搜索树)
题目如下所示:返回的结果是一个Node的Vector: Given n, generate all structurally unique BST's (binary search trees) th ...
随机推荐
- bootstrap 模态框(modal)插件使用
今天用户登陆时,在原网页上弹出新登陆窗口,发现使用的是modal插件,记录下该插件的使用方法,手写强化下. 首先,模态框(modal)是覆盖在父窗体上的子窗体,目的是显示来自一个单独的源的内容,可以在 ...
- [福大软工] W班 总成绩排行榜
评分链接 作业1 作业2 作业3 作业4 总分排名
- 基于Python的Web应用开发实践总结
基于Python的Web应用开发学习总结 项目地址 本次学习采用的是Flask框架.根据教程开发个人博客系统.博客界面如图所示. 整个学习过程收获很多,以下是学习总结. 1.virtualenv ...
- 201621123057 《Java程序设计》第2周学习总结
一.本周学习总结 基本数据类型 char实质属于整型.boolean类型取值只有true和false两种. 引用数据类型 包装类:自动装箱 与 自动拆箱 数组:一维数组遍历数组用foreach循环:多 ...
- Ubuntu下tomcat或eclipse启动提示没有java环境问题
tomcat和eclipse默认使用了openjdk,通过压缩包安装的jdk无法被识别,通过修改tomcat/bin下的catalina.sh添加jdk和jre路径即可 sudo gedit cata ...
- hibernate.QueryException: ClassNotFoundException: org.hibernate.hql.ast.HqlToken
环境:weblogic10.3.5,hibernate3,GGTS(groovy/grails tools suite):出现这问题是因为该项目是从weblogic8.1.6下移植到weblogic1 ...
- Codeforces 240 F. TorCoder
F. TorCoder time limit per test 3 seconds memory limit per test 256 megabytes input input.txt output ...
- Raid5两块硬盘掉线可以恢复数据吗_raid数据恢复案例分享
本案例中发生故障的存储类型是HP P2000,虚拟化平台为vmware exsi,共有10块硬盘组成raid5(硬盘容量为1t,其中6号盘是热备盘),由于某些故障导致阵列中两块硬盘亮黄灯掉线,硬盘无法 ...
- nyoj 回文字符串
回文字符串 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba".当 ...
- Python习题(第一课)
想了想其他的太简单了,还是不放了,剩三题吧. 一.完美立方 编写一个程序,对任给的正整数N (N≤100),寻找所有的四元组(a, b, c, d),使得a^3= b^3 + c^3 + d^3,其中 ...