[抄题]:

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左节点

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

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

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

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

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

  4. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  5. [LeetCode] Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

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

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

  8. [leetcode]99. Recover Binary Search Tree恢复二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  9. [Leetcode] Recover binary search tree 恢复二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

随机推荐

  1. 【转】关于gcc、glibc和binutils模块之间的关系

    原文网址:http://www.mike.org.cn/articles/linux-about-gcc-glibc-and-binutils-the-relationship-between-mod ...

  2. CentOS部署NetCore - 1. 安装CentOS 7 & 安装 Nginx

    1. 环境: Win7 64bit 2. 准备: VMWare(12.0.0 build-2985596) CentOS 7 Minimal ISO (http://isoredirect.cento ...

  3. 配置Jar包及相关依赖Jar包的本地存放路径

    配置Jar包及相关依赖Jar包的本地存放路径 用 maven2 ,pom.xml中设置了依赖,会帮你下载所有依赖的.jar到 M2_REPO 指向的目录. M2_REPO是一个用来定义 maven 2 ...

  4. 小小的学习FPGA建议

    学习FPGA,一点小小的 建议或者总结分享. 语法层面搞懂阻塞和非阻塞语句,以及Verilog语言的时序描述方法,把自己想象成编译器,尝试去编译自己写的Module,不断总结自己设计的逻辑会综合出怎么 ...

  5. Linux:数据库服务(Mysql安装及链接、远程链接、genelog)

    yum  search  +  服务:查询服务是否存在: yum  remove  +  服务:卸载服务: 使用 service 操作服务时,服务的名称后要加上字符 d,如启动:service  my ...

  6. java web 程序---登陆验证session。提示登陆

    loigin.jsp <%@ page language="java" import="java.util.*" pageEncoding="g ...

  7. 多分类下的ROC曲线和AUC

    本文主要介绍一下多分类下的ROC曲线绘制和AUC计算,并以鸢尾花数据为例,简单用python进行一下说明.如果对ROC和AUC二分类下的概念不是很了解,可以先参考下这篇文章:http://blog.c ...

  8. 小程序scroll-view组件使用时,子元素虽设置样式display:inline-flex;whit-space:nowrap

    小程序scroll-view组件使用时,子元素虽设置样式display:inline-flex;whit-space:nowrap

  9. 如何通过.Net Compact Framework来获得应用程序的当前路径

    在Win CE上是没有驱动器的概念的,所以要想使用System.IO.Directory.GetCurrentDirectory()来获得当前路径的话,在CF中会遇到未知错误.   应该使用Path. ...

  10. jQuery中this与$(this)的区别实例

    <p><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "<a hre ...