1. 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
给出的是二叉搜索树,先复习下什么是二叉搜索树
- 1.也就说一个根节点只有两个子树
- 2.左子树 < 根节点 < 右子树
 
题目的大概意思就是删除`[L, R]`范围外的节点
  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public TreeNode trimBST(TreeNode root, int L, int R) {
  12. if(root == null || L > R) return null; //递归的出口
  13. if(root.val < L)return trimBST(root.right,L,R);
  14. if(root.val > R)return trimBST(root.left,L,R);
  15. root.left = trimBST(root.left,L,R);
  16. root.right = trimBST(root.right,L,R);
  17. return root;
  18. }
  19. }
 
 
 
 
 
 

669. Trim a Binary Search Tree的更多相关文章

  1. 【Leetcode_easy】669. Trim a Binary Search Tree

    problem 669. Trim a Binary Search Tree 参考 1. Leetcode_easy_669. Trim a Binary Search Tree; 完

  2. Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees

    Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees 669.Trim a Binary Search Tr ...

  3. [Leetcode]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 that a ...

  4. LeetCode 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 t ...

  5. [LeetCode&Python] Problem 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 that a ...

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

  7. LeetCode: 669 Trim a Binary Search Tree(easy)

    题目: Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so th ...

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

  9. 【LeetCode】669. Trim a Binary Search Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

随机推荐

  1. MongoDB中聚合工具Aggregate等的介绍与使用

    Aggregate是MongoDB提供的众多工具中的比较重要的一个,类似于SQL语句中的GROUP BY.聚合工具可以让开发人员直接使用MongoDB原生的命令操作数据库中的数据,并且按照要求进行聚合 ...

  2. Self Hosting WebServer 的几种方式

    写在前面: IIS是Windows平台非常关键的组件,它是微软自带的Web服务器,可以很方便的帮助我们运行起一个网站,WebApi等服务,提供给外部来访问.即使它被很多java或者ruby的同学各种鄙 ...

  3. 逻辑运算&数据

    数据在计算机中只是0和1而已 数据在我们的理论中可以无穷大,但是在计算机中并不是,毕竟硬盘是有大小的. 具体可以通过一张图来理解 例如,0-F的表示 上面是有符号数,那么无符号数则是 事实上,计算机中 ...

  4. 中文编程语言之Z语言初尝试: ZLOGO 4

    原文: https://zhuanlan.zhihu.com/p/31505895. 作者为本人. @TKT2016 开发的Z语言(ZLOGO是它的一个部分)是本人至今看到的唯一一个仍活跃开发的开源且 ...

  5. PHP招聘那些事,公司真的不需要培训班出来的人么?

    就业形势严峻的情况下,每个企业对于人才的需求都不一样,并不是说公司不愿意招聘培训班出来的人,而是看你的能力是不是能胜任企业招聘人才的需求,是不是能给企业带来价值的人. 现在市面上的培训机构多如牛毛,然 ...

  6. C#的数据类型总结(2):decimal ,double,float的区别

    1> 三者是精度不同的浮点数,如下图 参见:https://docs.microsoft.com/zh-cn/dotnet/articles/csharp/language-reference/ ...

  7. 关于promise的详细讲解

    到处是回调函数,代码非常臃肿难看, Promise 主要用来解决这种编程方式, 将某些代码封装于内部. Promise 直译为"承诺",但一般直接称为 Promise; 代码的可读 ...

  8. Android 7.1 WindowManagerService 屏幕旋转流程分析 (二)

    一.概述 从上篇[Android 7.1 屏幕旋转流程分析]知道实际的旋转由WindowManagerService来完成,这里接着上面具体详细展开. 调了三个函数完成了三件事,即首先调用update ...

  9. Python爬虫(九)_非结构化数据与结构化数据

    爬虫的一个重要步骤就是页面解析与数据提取.更多内容请参考:Python学习指南 页面解析与数据提取 实际上爬虫一共就四个主要步骤: 定(要知道你准备在哪个范围或者网站去搜索) 爬(将所有的网站的内容全 ...

  10. Python爬虫(十六)_JSON模块与JsonPath

    本篇将介绍使用,更多内容请参考:Python学习指南 数据提取之JSON与JsonPATH JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,它是的人们很容易 ...