[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 ...
随机推荐
- 获取dmp文件的schema
白天的时候,做了一个获取dmp文件的schema实验,特此记录一下. 参考文章:如何获取dmp文件的schema -- by 我的烟灰缸 http://oradb.cc/2017/07/10/%E5 ...
- iOS企业版使用第三方实现自动更新版本
1.获取本地版本和互联网版本 NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; N ...
- React demo:express、react-redux、react-router、react-roter-redux、redux-thunk(二)
上一篇杂七杂八说了下express部分的,现在开始进入正题. 接下去的顺序,就是项目从零开始的顺序(思路方向). [actions定义] 如图,目录页,有4部分的内容,所以以下几个actions是需要 ...
- 大数据hadoop面试题2018年最新版(美团)
还在用着以前的大数据Hadoop面试题去美团面试吗?互联网发展迅速的今天,如果不及时更新自己的技术库那如何才能在众多的竞争者中脱颖而出呢? 奉行着"吃喝玩乐全都有"和"美 ...
- 移动端Web资源整合
meta基础知识 H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 <meta name="viewport" content="width=device-wid ...
- u3d材质Tiling和Offset意义以及TRANSFORM_TEX
1. TRANSFORM_TEX主要作用是拿顶点的uv去和材质球的tiling和offset作运算, 确保材质球里的缩放和偏移设置是正确的 下面这两个函数是等价的.o.uv = TRANSFORM ...
- JavaWeb学习笔记二 Http协议和Tomcat服务器
Http协议 HTTP,超文本传输协议(HyperText Transfer Protocol),是互联网上应用最为广泛的一种网络协议.所有的WWW文件都必须遵守这个标准.设计HTTP最初的目的是为 ...
- node初始
### 一.什么是node.js > Node是一个基于 Chrome V8 引擎的 JavaScript 运行环境 > > Node使用了一个事件驱动.非阻塞式 I/O 的模型,使 ...
- 项目Alpha冲刺Day3
一.会议照片 二.项目进展 1.今日安排 服务器后台基本搭建完成,完成帐号权限一小部分完成并进行框架使用练手. 2.问题困难 跨专业成员不熟java的开发,有一名成员之前主要做安卓的,所以有比较多的东 ...
- 【iOS】swift 排序Sort函数用法(包含NSDictionary排序)
用了几分钟做的简单翻译 一个例子 直接贴代码,不过多解释 //这是我们的model class imageFile { var fileName = String() var fileID = Int ...