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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
def trimBST(self, root, L, R):
"""
:type root: TreeNode
:type L: int
:type R: int
:rtype: TreeNode
"""
if root==None:
return root if R<root.val:
return self.trimBST(root.left,L,R) if L>root.val:
return self.trimBST(root.right,L,R) root.left=self.trimBST(root.left,L,R)
root.right=self.trimBST(root.right,L,R) return root

  

[LeetCode&Python] Problem 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 解题报告(Python)

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

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

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

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

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

随机推荐

  1. robot切换窗口

    在用robot做自动化测试的时候,经常遇到新开窗口,需要切换窗口的情况. 两种方式来解决: (1)用robot的关键字来解决 实现窗口切换的功能 @{handlelist} List Windows ...

  2. SpringBoot 中常用注解@Controller/@RestController/@RequestMapping介绍

    原文 SpringBoot 中常用注解 @Controller/@RestController/@RequestMapping介绍 @Controller 处理http请求 @Controller / ...

  3. freemarker中对null值问题的处理

    1. freemarker不支持null. 如果值为null会报错. 2.当值为null的处理 1)过滤不显示 Hello ${name!} 在属性后面加感叹号即可过滤null和空字符串 if和”?? ...

  4. ArcGIS Runtime数据制作教程

    分类: ArcGIS Runtime 2012-04-20 22:25 879人阅读 评论(0) 收藏 举报 工具测试文档file工作c ArcGIS Runtime不能直接加载shp数据,或者mxd ...

  5. java.lang.RuntimeException: Unable to start activity ComponentInfo{com.autumn.book/com.autumn.book.MainActivity}: android.os.NetworkOnMainThreadException

    不能把http请求写在主线程里,改为这样 Runnable runnable = new Runnable() { public void run() { HttpClient.post2(" ...

  6. [NOI2018]你的名字(68pts)

    SAM真让人头秃. 题面 https://www.luogu.org/problemnew/show/P4770 首先考虑 l=1,r=∣S∣的做法 如果对于ION2018的子串不用判重的话,对ION ...

  7. Codeforces Round #364 (Div. 1) (差一个后缀自动机)

    B. Connecting Universities 大意: 给定树, 给定2*k个点, 求将2*k个点两两匹配, 每个匹配的贡献为两点的距离, 求贡献最大值 单独考虑每条边$(u,v)$的贡献即可, ...

  8. linux 查找

    linux下的查找命令有很多,常用的有which.whereis.locate.find.平时在网上搜索一些教程的时候,经常会有让使用这四条命令中的一条去查找一些东西,但是经常这次说用which,下次 ...

  9. 微信小程序自定义模态框(字体图标)

    组件已经传到github,自行下载:https://github.com/JinZhenZon/miniapp-customModel 支持如下配置: {  outWidth <number&g ...

  10. 关于Floyd求解最小环的问题

    最近学习了floyd的奇妙用处,求解最小环,自己的领悟写在了纸上. 对于一个最小环,显然至少要包含三个点(此处不把两个点的回路称之为环) 从大体上考虑的话,一定有一个点与左右两侧的点是直接连接的(即不 ...