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:

  1. Input:
  2. 1
  3. / \
  4. 0 2
  5.  
  6. L = 1
  7. R = 2
  8.  
  9. Output:
  10. 1
  11. \
  12. 2

Example 2:

  1. Input:
  2. 3
  3. / \
  4. 0 4
  5. \
  6. 2
  7. /
  8. 1
  9.  
  10. L = 1
  11. R = 3
  12.  
  13. Output:
  14. 3
  15. /
  16. 2
  17. /
  18. 1

这道题让我们修剪一棵二叉搜索树,给了个边界范围[L, R], 所有不在这个范围内的结点应该被移除掉,但是仍需要保留二叉搜索树的性质,即左<根<右,有时候是小于等于。博主最开始的想法是先遍历一遍二叉树,将在返回内的结点值都放到一个数组后,遍历结束后再根据数组重建一棵二叉搜索树。这种方法会在某些test case上fail掉,可能会改变原来的二叉搜索树的结构,所以我们只能换一种思路。正确方法其实应该是在遍历的过程中就修改二叉树,移除不合题意的结点。当然对于二叉树的题,十有八九都是要用递归来解的。首先判断如果root为空,那么直接返回空即可。然后就是要看根结点是否在范围内,如果根结点值小于L,那么返回对其右子结点调用递归函数的值;如果根结点大于R,那么返回对其左子结点调用递归函数的值。如果根结点在范围内,将其左子结点更新为对其左子结点调用递归函数的返回值,同样,将其右子结点更新为对其右子结点调用递归函数的返回值。最后返回root即可,参见代码如下:

解法一:

  1. class Solution {
  2. public:
  3. TreeNode* trimBST(TreeNode* root, int L, int R) {
  4. if (!root) return NULL;
  5. if (root->val < L) return trimBST(root->right, L, R);
  6. if (root->val > R) return trimBST(root->left, L, R);
  7. root->left = trimBST(root->left, L, R);
  8. root->right = trimBST(root->right, L, R);
  9. return root;
  10. }
  11. };

下面这种方法是迭代的写法,虽然树的题一般都是用递归来写,简洁又美观。但是我们也可以强行用while来代替递归,比如下面这种写法:

解法二:

  1. class Solution {
  2. public:
  3. TreeNode* trimBST(TreeNode* root, int L, int R) {
  4. if (!root) return NULL;
  5. while (root->val < L || root->val > R) {
  6. root = (root->val < L) ? root->right : root->left;
  7. }
  8. TreeNode *cur = root;
  9. while (cur) {
  10. while (cur->left && cur->left->val < L) {
  11. cur->left = cur->left->right;
  12. }
  13. cur = cur->left;
  14. }
  15. cur = root;
  16. while (cur) {
  17. while (cur->right && cur->right->val > R) {
  18. cur->right = cur->right->left;
  19. }
  20. cur = cur->right;
  21. }
  22. return root;
  23. }
  24. };

参考资料:

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. 获取dmp文件的schema

    白天的时候,做了一个获取dmp文件的schema实验,特此记录一下. 参考文章:如何获取dmp文件的schema  -- by 我的烟灰缸 http://oradb.cc/2017/07/10/%E5 ...

  2. iOS企业版使用第三方实现自动更新版本

    1.获取本地版本和互联网版本          NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];     N ...

  3. React demo:express、react-redux、react-router、react-roter-redux、redux-thunk(二)

    上一篇杂七杂八说了下express部分的,现在开始进入正题. 接下去的顺序,就是项目从零开始的顺序(思路方向). [actions定义] 如图,目录页,有4部分的内容,所以以下几个actions是需要 ...

  4. 大数据hadoop面试题2018年最新版(美团)

    还在用着以前的大数据Hadoop面试题去美团面试吗?互联网发展迅速的今天,如果不及时更新自己的技术库那如何才能在众多的竞争者中脱颖而出呢? 奉行着"吃喝玩乐全都有"和"美 ...

  5. 移动端Web资源整合

    meta基础知识 H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 <meta name="viewport" content="width=device-wid ...

  6. u3d材质Tiling和Offset意义以及TRANSFORM_TEX

    1. TRANSFORM_TEX主要作用是拿顶点的uv去和材质球的tiling和offset作运算, 确保材质球里的缩放和偏移设置是正确的 下面这两个函数是等价的.o.uv =   TRANSFORM ...

  7. JavaWeb学习笔记二 Http协议和Tomcat服务器

    Http协议 HTTP,超文本传输协议(HyperText Transfer Protocol),是互联网上应用最为广泛的一种网络协议.所有的WWW文件都必须遵守这个标准.设计HTTP最初的目的是为 ...

  8. node初始

    ### 一.什么是node.js > Node是一个基于 Chrome V8 引擎的 JavaScript 运行环境 > > Node使用了一个事件驱动.非阻塞式 I/O 的模型,使 ...

  9. 项目Alpha冲刺Day3

    一.会议照片 二.项目进展 1.今日安排 服务器后台基本搭建完成,完成帐号权限一小部分完成并进行框架使用练手. 2.问题困难 跨专业成员不熟java的开发,有一名成员之前主要做安卓的,所以有比较多的东 ...

  10. 【iOS】swift 排序Sort函数用法(包含NSDictionary排序)

    用了几分钟做的简单翻译 一个例子 直接贴代码,不过多解释 //这是我们的model class imageFile { var fileName = String() var fileID = Int ...