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 这道题描述的需求是:
给我们一个二叉排序树 最小数l 和最大数r
我们需要做的是对这棵树进行裁剪,让树里所有的节点值都满足在l和r之间 思想:
二叉排序树的特点是:对于任意一个节点,左子树任意节点数值都比跟节点小,右子树任意节点数值都比根大
所以考虑,对于任意一个节点,
如果值比l还小,那就应该抛弃这个根,去右子树寻找新的根
如果值比r还大,那就应该抛弃这个根,去左子树寻找新的根 这样的操作进行递归就可以了 我的python代码:
 # 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 is None:
return None
if root.val >R:
return self.trimBST(root.left ,L,R)
if root.val<L:
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 if __name__ == '__main__':
s = Solution() root = TreeNode(1)
root.left = TreeNode(0)
root.right = TreeNode(2) print(root.val,root.left.val,root.right.val) root = s.trimBST(root,1,2) print(root, root.left, root.right)
												

leetcode算法:Trim a Binar Search Tree的更多相关文章

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

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

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

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

  5. [leetcode]669. Trim a Binary Search Tree寻找范围内的二叉搜索树

    根据BST的特点,如果小于L就判断右子树,如果大于R就判断左子树 递归地建立树 public TreeNode trimBST(TreeNode root, int L, int R) { if (r ...

  6. LeetCode 669. 修剪二叉搜索树(Trim a Binary Search Tree)

    669. 修剪二叉搜索树 669. Trim a Binary Search Tree 题目描述 LeetCode LeetCode669. Trim a Binary Search Tree简单 J ...

  7. leetcode算法: Find Bottom Left Tree Value

    leetcode算法: Find Bottom Left Tree ValueGiven a binary tree, find the leftmost value in the last row ...

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

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

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

随机推荐

  1. 使用uiautomation自动化重命名pdf书签,使全大写字母变成首字母大写

    今天下载了一个英文pdf书籍,但书签全是大写英文字母,看上去有点别扭,于是想办法用自动化重命名pdf书签, 使书签全部变成首字母大写. pdf原始书签如下图: 重命名后的pdf书签 自动化动态效果图, ...

  2. Sublime + Python3 + 虚拟环境 + 去除 中文输出乱码

    MacBook Pro Retina 13 2013年底版 所用软件 1. Sublime Text 3安装 Virtualenv package 2. 用 iterm2 .或者终端安装zip:apt ...

  3. mac下利用Breakpad的dump文件进行调试

    一.前情回顾 最近把公司的一个视频处理程序更新了一个版本,准备提交测试的发现了崩溃的情况.这个程序采用Qt和ffmpeg技术栈开发,主要用于对视频进行渲染拼接处理,在Windows和mac两个平台同时 ...

  4. 1-1 struts2 基本配置 struts.xml配置文件详解

    详见http://www.cnblogs.com/dooor/p/5323716.html 一. struts2工作原理(网友总结,千遍一律) 1 客户端初始化一个指向Servlet容器(例如Tomc ...

  5. eclipse的Debug模式下的快捷键

    主要快捷键: F5, F6, F7, F8的使用 F5:  进入当前方法 F6: 一步步执行 F7:  跳出方法, 返回到调用此方法的最后一条语句 F8: 继续执行,跳转到下一个断点的位置 示例: 在 ...

  6. 【日记】一次程序调优发现的同步IO写的问题,切记

    众所周知,我们在写程序的时候,好习惯是在重要的代码打上日志.以便监控程序运行的性能和记录可能发生的错误. 但是,如果日志是基于同步IO文件操作,那么就必须考虑到访问总次数或并发数目. 如果总次数或并发 ...

  7. python分支

    if xxx : xxxxx elif xxxx : xxxxx elif xxxx : xxxxx else: xxxxxx 分支可以有效节省CPU的运算时间.避免悬挂else的出现 三元表达式 s ...

  8. Object的方法

    1.Object.assign() 方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象.它将返回目标对象. ES2015引入的 ,且可用polyfilled.要支持旧浏览器的话,可用使用jQ ...

  9. linux系统命令学习-用户管理

    1. 用户 a)  系统使用user id 简称UID来标志用户的唯一性 b)  用户分为三类:系统用户,根用户,普通用户 i. 普通用户 UID大于500,系统默认普通用户UID从500开始 只能操 ...

  10. 如何用Word编辑参考文献------这是引用一位大师的

    如何用Word编辑参考文献修改文献是一件非常痛苦的事情,虽然现在也有很多软件可以编排参考文献,其实word本身就可以. 采用合适的编辑方法会方便地做到整齐,规范,自动排序和交叉引用.1.以尾注的方式插 ...