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. # Definition for a binary tree node.
  2. # class TreeNode:
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.left = None
  6. # self.right = None
  7.  
  8. class Solution:
  9. def trimBST(self, root, L, R):
  10. """
  11. :type root: TreeNode
  12. :type L: int
  13. :type R: int
  14. :rtype: TreeNode
  15. """
  16. if root==None:
  17. return root
  18.  
  19. if R<root.val:
  20. return self.trimBST(root.left,L,R)
  21.  
  22. if L>root.val:
  23. return self.trimBST(root.right,L,R)
  24.  
  25. root.left=self.trimBST(root.left,L,R)
  26. root.right=self.trimBST(root.right,L,R)
  27.  
  28. 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. 编写自己的代码库(javascript常用实例的实现与封装)[转]

    1.前言 因为公司最近项目比较忙,没那么多空余的事件写文章了,所以这篇文章晚了几天发布.但是这也没什么关系,不过该来的,总是会来的.好了,其他的不多说的,大家在开发的时候应该知道,有很多常见的实例操作 ...

  2. URAL 1501 Sense of Beauty

    URAL 1501 思路: dp+记忆化搜索 状态:dp[i][j]表示选取第一堆前i个和第二堆前j的状态:0:0多1个              1:0和1相等                2:1 ...

  3. Codeforces 559B - Equivalent Strings

    559B - Equivalent Strings 思路:字符串处理,分治 不要用substr(),会超时 AC代码: #include<bits/stdc++.h> #include&l ...

  4. C#中类的序列化和反序列化

    说明:本文演示将类序列化后写入记事本并从记事本读取反序列化为对象1.首先创建一个类,同时类必须标识为Serializable,如下: [Serializable] public class Regio ...

  5. IIS中发布后出现Could not load file or assembly'System.Data.SQLite.dll' or one of its depedencies

    [问题]在我本机的开发环境c#连接sqlite3没有问题,可是release版本移植到其他的机器就提示Could not load file or assembly'System.Data.SQLit ...

  6. English trip -- VC(情景课) 7 A Shopping 购物

    Words The clothes place a dress 长裙      short skirt 短裙 pants 裤子   /  trousers 长裤  / shorts 短裤 a shir ...

  7. (转)nginx做转发时,带'_'的header内容丢失

    原本在测试环境测试通过的APP,今天准备切到线上环境做最后测试,结果发现了错误.查看日志发现是APP端发送的http请求中的header内容丢失了.那么代码没有改动,怎么平白无故会丢失头信息? 于是想 ...

  8. 49 DOM(2)

    一.value属性: input ,select 标签 ,textarea 标签中有value属性, 获取他们属性值的方法,先获取该元素ele,然后ele.value得到value值. <!DO ...

  9. Python基础--列表、元组

    一.什么是列表.元组 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置类型,但最常见 ...

  10. 操作系统错误 5:"5(拒绝访问。)

    ------------------------------ 无法打开物理文件 "G:/QGJX.mdf".操作系统错误 5:"5(拒绝访问.)". (Micr ...