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 ...
随机推荐
- 【BZOJ】4292: [PA2015]Równanie
题解 \(f(n)\)的取值范围最多\(9^2 * 18\) 直接枚举判断就好 代码 #include <bits/stdc++.h> #define fi first #define s ...
- Zookeeper Canary
ZooKeeper Canary我理解用于测试ZK节点是否正常的心跳服务,这是从ClouderaManager中粘下来的: 这是 ZooKeeper 服务级运行状况测试,用于检查基本客户端操作是否正常 ...
- CF1010D Mars rover [位运算,DP]
题目传送门 Mars Rover 格式难调,题面就不放了. 分析: 今天考试的时候考了这道题目的加强版,所以来做. 其实也并不难,我们建立好树形结构以后先把初始权值全部求出,然后就得到了根节点的初始值 ...
- iOS 11开发教程(十七)iOS11应用视图之使用按钮接收用户输入
iOS 11开发教程(十七)iOS11应用视图之使用按钮接收用户输入 在iOS中提供了很多的控件以及视图来丰富用户界面,对于这些视图以及控件我们在上一章中做了简单的介绍.本章我们将详细讲解这些视图. ...
- Uncaught Error: Syntax error, unrecognized expression: [flag=]报错处理方法
今早运行项目的时候报这个错误: 百度翻译的解释是:未命名错误:语法错误,未识别表达式:[FLAG= ] 也就是书写规范问题. 可是我查了对应的js: 字符串拼接没什么问题,经常这样写. 这时看报错 ...
- 吴恩达-coursera-机器学习-week8
十三.聚类(Clustering) 13.1 无监督学习:简介 13.2 K-均值算法 13.3 优化目标 13.4 随机初始化 13.5 选择聚类数 十四.降维(Dimensionality Red ...
- SQL Server Management Studio 教程二: 创建新登录名
1.先用windows身份登录SQL server2008 2.打开[安全性],右击[登录名],选择[新建登录名] 3.[常规]选项页面中,修改如下位置设置,默认数据库可以是其他数据库,不一定是mas ...
- j.u.c系列(11)---之并发工具类:Exchanger
写在前面 前面三篇博客分别介绍了CyclicBarrier.CountDownLatch.Semaphore,现在介绍并发工具类中的最后一个Exchange.Exchange是最简单的也是最复杂的,简 ...
- CSS动画简介
现在,我很少写介绍CSS的文章,因为感觉网站开发的关键还是在服务器端. 但是,CSS动画除外,它实在太有用了. 本文介绍CSS动画的两大组成部分:transition和animation.我不打算给出 ...
- .Net中常用的重要的第三方组件
RSS.NET.dll RSS.NET是一款操作RSS feeds的开源.NET类库.它为解析和编写RSS feeds提供了一个可重用的对象模型.它完全兼容RSS 0.90, 0.91, 0.92, ...