Binary Search Tree 以及一道 LeetCode 题目
一道LeetCode题目
今天刷一道LeetCode的题目,要求是这样的:
Given a binary search tree and the lowest and highest boundaries as
L
andR
, 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.
由于对于 Binary search tree 不理解,所以绕了点弯路,要解这道题,必须理解什么是 binary search tree。我们来看一下定义:
A binary search tree is a rooted binary tree, whose internal nodes each store a key (and optionally, an associated value) and each have two distinguished sub-trees, commonly denoted left and right. The tree additionally satisfies the binary search property, which states that the key in each node must be greater than or equal to any key stored in the left sub-tree, and less than or equal to any key stored in the right sub-tree.[1]:287 (The leaves (final nodes) of the tree contain no key and have no structure to distinguish them from one another.
看一下下面这个图一下子就能理解,就是说每个节点左边的值一定小于右边。
了解到这个约束,这个题目解起来就比较简单了:
class Solution:
def trimBST(self, root, L, R):
"""
:type root: TreeNode
:type L: int
:type R: int
:rtype: TreeNode
"""
if(root.val < L):
if(root.right != None):
root = self.trimBST(root.right, L, R)
else:
return None
elif(root.val > R):
if(root.left != None):
root = self.trimBST(root.left, L, R)
else:
return None
else:
if(root.left != None):
root.left = self.trimBST(root.left, L, R)
else:
root.left = None
if(root.right != None):
root.right = self.trimBST(root.right, L, R)
else:
root.right = None
return root
BST数据结构的一些算法特性
Algorithm | Average | Worst case |
---|---|---|
Space | O(n) | O(n) |
Search | O(log n) | O(n) |
Insert | O(log n) | O(n) |
Delete | O(log n) | O(n) |
参考资料:
1、Leetcode
2、Wiki Binary search tree
Binary Search Tree 以及一道 LeetCode 题目的更多相关文章
- [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- PAT_A1043#Is It a Binary Search Tree
Source: PAT A1043 Is It a Binary Search Tree (25 分) Description: A Binary Search Tree (BST) is recur ...
- PAT_A1064#Complete Binary Search Tree
Source: PAT A1064 Complete Binary Search Tree (30 分) Description: A Binary Search Tree (BST) is recu ...
- PAT_A1099#Build A Binary Search Tree
Source: PAT A1099 Build A Binary Search Tree (30 分) Description: A Binary Search Tree (BST) is recur ...
- 【一天一道LeetCode】#235. Lowest Common Ancestor of a Binary Search Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【一天一道LeetCode】#109. Convert Sorted List to Binary Search Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【一天一道LeetCode】#99. Recover Binary Search Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Two ele ...
- 【一天一道LeetCode】#98. Validate Binary Search Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
随机推荐
- JQuery表格插件
http://www.datatables.club/example/#styling Datatables快速入门开发--一款好用的JQuery表格插件 博主是一个java后端程序员,前端技术会 ...
- Hive知识汇总
两种Hive表 hive存储:数据+元数据 托管表(内部表) 创建表: hive> create table test2(id int,name String,tel String) > ...
- 高能天气——团队Scrum冲刺阶段-Day 1-领航
高能天气--团队Scrum冲刺阶段-Day 1-领航 各个成员在 Alpha 阶段认领的任务 经过重新的团队讨论,我们最新确定的α版本所需实现内容如下: 查找城市:切换城市按钮.滑动界面视图 天气预报 ...
- C#并行编程(4):基于任务的并行
C#中的任务Task 在C#编程中,实现并行可以直接使用线程,但使用起来很繁琐:也可以使用线程池,线程池很大程度上简化了线程的使用,但是也有着一些局限,比如我们不知道作业什么时候完成,也取不到作业的返 ...
- BEM
BEM代表块(Block),元素(Element),修饰符(Modifier).BEM是由Yandex团队提出的一种CSS Class 命名方法,旨在更好的创建CSS/Sass模块.通过增加前缀的办法 ...
- Linux系统的组成
<linux系统7大子系统> a:SCI(system call interface) ————用户程序通过软件中断后,调用系统内核提供的功能,这个在用户空间和内核提供的服务之间的接口称为 ...
- BZOJ.3058.四叶草魔杖(Kruskal 状压DP)
题目链接 \(2^{16}=65536\),可以想到状压DP.但是又有\(\sum A_i\neq 0\)的问题.. 但是\(2^n\)这么小,完全可以枚举所有子集找到\(\sum A_i=0\)的, ...
- 【学习笔记】python的代码块(吐槽)
曾经我以为python是像pascal那样begin开始end结束, 直到今天…… 我才知道python是用缩进作为代码段标识的…… >>> def test(n): ... if ...
- error/exception/runtime exception区别
(1)java中的异常是什么? 异常指的是程序运行过程中出现的非正常情况或错误,当程序违反了语义规则时,jvm就会将出现的错误表示为一个异常抛出.在java中,一切皆对象,异常也是,它被当作一个对象, ...
- Slickflow.NET 开源工作流引擎高级开发(一) -- 流程外部事件的调用和变量存储实现
前言:流程实现基本流转功能外,通常也需要调用外部事件,用于和业务系统的交互,同时存储一些流程变量,用于追踪和记录业务数据变化对流程流转的影响. 1. 流程事件 流程执行过程中,伴随各种事件的发生,而且 ...