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 修剪一棵二叉搜索树的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 109 Convert Sorted List to Binary Search Tree 有序链表转换二叉搜索树

    给定一个单元链表,元素按升序排序,将其转换为高度平衡的BST.对于这个问题,一个高度平衡的二叉树是指:其中每个节点的两个子树的深度相差不会超过 1 的二叉树.示例:给定的排序链表: [-10, -3, ...

  4. Leetcode109. Convert Sorted List to Binary Search Tree有序链表转换二叉搜索树

    给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定的有序链表: [-10 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. LeetCode OJ :Unique Binary Search Trees II(唯一二叉搜索树)

    题目如下所示:返回的结果是一个Node的Vector: Given n, generate all structurally unique BST's (binary search trees) th ...

随机推荐

  1. Oracle查询优化改写--------------------操作多个表

    一.union all与空字符串 二.组合相关行 三.in .exists.inter join .left join .right join .full join 之间的区别 'inner  joi ...

  2. Catch That Cow_bfs

    Catch That Cow 题目大意:FrammerJohn找奶牛,给出n和k.FJ在n处.每次他可以向左移动一格.向右移动一格或者移动到自己当前格子数乘2的地方.求FJ最少移动多少次.其中,FJ和 ...

  3. 升级 mysql5.6 配置文件my.cnf sql_mode 解析与设置问题

    sql_mode是个很容易被忽视的变量,默认值是空值,在这种设置下是可以允许一些非法操作的,比如允许一些非法数据的插入.在生产环境必须将这个值设置为严格模式,所以开发.测试环境的数据库也必须要设置,这 ...

  4. win10下NeuralStyle的tensorflow版实验

    ---恢复内容开始--- 首先配置win10下的tensorflow-gpu的运行环境,然后在github上将NeuralStyle拷贝下来,最后根据文档说明参数,运行文件,即可得到自己喜欢的styl ...

  5. 解决python中flask_sqlalchemy包安装失败的问题

    在进行flask_sqlalchemy包的下载安装时出现以下问题: 由图片可看出是编码转换出了问题,找到pip\compat_init_.py文件,打开它并查看第73行,将代码做如下更改并保存: 问题 ...

  6. 学号:201621123032 《Java程序设计》第4周学习总结

    1:本周学习总结 1. 写出你认为本周学习中比较重要的知识点关键词 继承,多态,父类object,抽象类 2. 尝试使用思维导图将这些关键词组织起来 2:书面作业 2.1: 面向对象设计 1. 讲故事 ...

  7. python 操作MongoDB

    安装MongoDB 启动数据库:安装完成指定数据库存放路径 mongod.exe --dbpath c:\data\db进入目录后运行mongo.exe 成功 创建数据库 > use mydb ...

  8. CISCO路由器练习

    前言: 总结了昨天的学习和今天的单臂路由 写了今天的文章. 目录: 路由器的基本配置 单臂路由的练习 正文: 路由器基本配置 环境要求 cisco模拟器 2台交换机 2台PC 1台路由器 路由器介绍: ...

  9. 06_Python的数据类型3元组,集合和字典_Python编程之路

    上一节跟大家讲了Python的列表,当然不是完整的讲完,后续我们还会提到,这一节我们还是来讲Python的数据类型 首先要讲到的就是元组 元组其实拥有列表的一些特性,可以存储不同类型的值,但在某些方面 ...

  10. php后台的在控制器中就可以实现阅读数增加

    $smodel=M('Sswz');$smodel->where($map)->setInc('view' ,1);php后台的在控制器中就可以实现阅读数增加前台不需要传值