leetcode算法:Trim a Binar Search Tree
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的更多相关文章
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- [leetcode]669. Trim a Binary Search Tree寻找范围内的二叉搜索树
根据BST的特点,如果小于L就判断右子树,如果大于R就判断左子树 递归地建立树 public TreeNode trimBST(TreeNode root, int L, int R) { if (r ...
- LeetCode 669. 修剪二叉搜索树(Trim a Binary Search Tree)
669. 修剪二叉搜索树 669. Trim a Binary Search Tree 题目描述 LeetCode LeetCode669. Trim a Binary Search Tree简单 J ...
- leetcode算法: Find Bottom Left Tree Value
leetcode算法: Find Bottom Left Tree ValueGiven a binary tree, find the leftmost value in the last row ...
- 【Leetcode_easy】669. Trim a Binary Search Tree
problem 669. Trim a Binary Search Tree 参考 1. Leetcode_easy_669. Trim a Binary Search Tree; 完
- 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 ...
随机推荐
- f.lux——自动调整屏幕色温减少眼睛疲劳,长时间玩电脑必备!
长时间玩电脑的同学肯定会觉得眼睛很难受,而电脑自带的调节亮度的功能通常又不能够满足我们,所以今天就给大家推荐一个保护视力的软件—— flux,这个软件是通过调节色温来达到保护视力的作用,通常在台式机的 ...
- Xamarin Forms中WebView的自适应高度
在Xamarin.Forms中,WebView如果嵌套在StackLayout和RelativeLayout中必须要设置HeightRequest和WidthRequest属性才会进行渲染.可是在实际 ...
- guava cache使用和源码分析
guava cache的优点和使用场景,用来判断业务中是否适合使用此缓存 介绍常用的方法,并给出示例,作为使用的参考 深入解读源码. guava简介 guava cache是一个本地缓存.有以下优点: ...
- ACCESS_ONCE
宏定义: #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x)) 分解: typeof(x):取x的类型,如果x是int,那typeof(x) ...
- 用C#语言编写:集合管理器
static void Main(string[] args) { List<int> numbers = new List<int>(); ...
- poj2793 素数和
题目链接:http://poj.org/problem?id=2739 #include<iostream> using namespace std; int count=0; int p ...
- Webpack结合ES6
一.概述ES6现在正是风华正茂的时候,各个公司都是 尝试去使用,并且作为前端工程师ES6也是体现技术的亮点.但是,现在的浏览器对es6支持不是 特别的兼容,最终还是需要把es6转换为es5,webpa ...
- webpack-dev-server 设置反向代理解决跨域问题
一.设置代理的原因 现在对前端开发的要求越来越高,并且随着自动化以及模块化的 诞生,前后端开发模式越来越流行.后端只负责接口,前端负责数据展示.逻辑处理.但是前后端开发模式,有一个重要的问题,就是跨域 ...
- 【CSS】 CSS基础知识 属性和选择
css基础知识 html的基本标签都是千篇一律的,为了能够个性化外观,就需要进行样式的调整,而css就是专门用来维护,管理样式的一种格式.在html中定义css有三种方法 1. 为标签添加style属 ...
- STL --> list用法
List介绍 Lists将元素按顺序储存在链表中.与 向量(vectors)相比, 它允许快速的插入和删除,但是随机访问却比较慢. assign() // 给list赋值 back() // 返回最后 ...