Leetcode 98. Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
- The left subtree of a node contains only nodes with keys less than the node's key.
- The right subtree of a node contains only nodes with keys greater than the node's key.
- Both the left and right subtrees must also be binary search trees.
Example 1:
2
/ \
1 3
Binary tree [2,1,3], return true.
Example 2:
1
/ \
2 3
Binary tree [1,2,3], return false.
对每一个节点使用一个upper bound and lower bound. 时间复杂度 O(N).
def isValidBST(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
maxInt = 2147483647
minInt = -2147483648 return self.isBSTHelper(root, minInt, maxInt) def isBSTHelper(self, root, minVal, maxVal):
if not root:
return True if root.val <= maxVal and root.val >= minVal and self.isBSTHelper(root.left, minVal, root.val - 1) and self.isBSTHelper(root.right, root.val + 1, maxVal):
return True
else:
return False
思路二:如果中序遍历一下tree,BST应该给出一个递增序列。注意中序遍历的非递归算法。
class Solution(object):
def isValidBST(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
ans = []
self.inOrder(root, ans) n = len(ans)
if n <= 1:
return True
for i in range(1,n):
if ans[i] <= ans[i-1]:
return False
return True def inOrder(self, root, ans):
if not root:
return ans
else:
self.inOrder(root.left, ans)
ans.append(root.val)
self.inOrder(root.right, ans)
Leetcode 98. Validate Binary Search Tree的更多相关文章
- [LeetCode] 98. Validate Binary Search Tree 验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- leetcode 98 Validate Binary Search Tree ----- java
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [leetcode]98. Validate Binary Search Tree验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [LeetCode] 98. Validate Binary Search Tree(是否是二叉搜索树) ☆☆☆
描述 解析 二叉搜索树,其实就是节点n的左孩子所在的树,每个节点都小于节点n. 节点n的右孩子所在的树,每个节点都大于节点n. 定义子树的最大最小值 比如:左孩子要小于父节点:左孩子n的右孩子要大于n ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- 【LeetCode】98. Validate Binary Search Tree (2 solutions)
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 【leetcode】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- leetcode dfs Validate Binary Search Tree
Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...
- 【一天一道LeetCode】#98. Validate Binary Search Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- PAT 1008. 数组元素循环右移问题 (20)
一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0 A1--AN-1)变换为(AN-M -- AN-1 A0 ...
- GLM in SPM
主要记一句话: SPM的GLM模型中的β,指的是相应regressor对最后测量得到的信号所产生的效应(effect). 后续的假设检验过程实际上都是对各个regressor的β向量进行的. The ...
- 苹果iPhone如何区分港版、国行、水货
要想分辨所购买的苹果产品[iPhone 4.iPod Touch.iPad 2.iMac.MacBook及iPhone 4S]是大陆行货.水货.港货还是其它,其实很简单.今天来教大家如何区分.大陆行货 ...
- Linux 网络编程详解八
TCP/IP协议三次握手机制 TCP/IP是全双工通道,两端都可以读写,三次握手机制就是验证TCP/IP是否是全双工通道 1.客户端调用connect()函数,阻塞客户端进程,客户端向服务器发送数据包 ...
- 数据库 SQL语法一
建立表语句 CREATE TABLE TABLENAME(COL_NAME1 TYPE,COL_NAME2 TYPE,......); 常用TYPE说明 INT 正数 CHAR(LENGTH) 定长字 ...
- python数字图像处理(17):边缘与轮廓
在前面的python数字图像处理(10):图像简单滤波 中,我们已经讲解了很多算子用来检测边缘,其中用得最多的canny算子边缘检测. 本篇我们讲解一些其它方法来检测轮廓. 1.查找轮廓(find_c ...
- 工作随笔——Java调用Groovy类的方法、传递参数和获取返回值
接触Groovy也快一年了,一直在尝试怎么将Groovy引用到日常工作中来.最近在做一个功能的时候,花了点时间重新看了下Java怎么调用Groovy的方法.传递参数和获取返回值. 示例Groovy代码 ...
- 给大一的学弟学妹们培训java web的后台开发讨论班计划
蓝旭工作室5月大一讨论班课程计划 课时 讨论班性质 讨论班名称 主要内容 主讲人 第一讲 先导课 后台开发工具的使用与MySQL数据库基础 后台开发工具的基本使用方法与工程的创建,MySQL数 ...
- 为什么Javascript中的基本类型能调用方法?
我们从一道笔试题说起: var str = 'string'; str.pro = 'hello'; console.log(str.pro + 'world'); 输出啥?要理解这个问题,我们得从头 ...
- Android一键多渠道分发打包实战和解析
当项目需要有更多的客户的时候,你就会考虑将apk上架到应用商店了,无奈天朝Android应用商店真的是百家争鸣,据某地不完全统计已经有900+.若将Apk上架到所有的应用商店是个好主意,但是据统计也就 ...