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 ...
随机推荐
- Mysql数据库之Binlog日志使用总结
binlog二进制日志对于mysql数据库的重要性有多大,在此就不多说了.下面根据本人的日常操作经历,并结合网上参考资料,对binlog日志使用做一梳理: 一.binlog日志介绍1)什么是binlo ...
- ESXi 5.5 解决 cannot edit the settings of virtual machines of version 10
ESXi 5.5 是VMWare提供的免费虚拟服务器软件, 因为其优秀的性能, 对CPU, 内存和虚拟机数量都解除了限制, 成为很多个人或者小型公司的首选虚拟化工具. 在日常管理时常碰到的一个问题是, ...
- 002商城项目:maven工程的测试以及svn的使用
我们上一篇文章搭建了maven工程,这一篇文章我们就要测试这个工程. 1: 由于这个工程还没有页面,我们要首先建立一个页面.在建立页面的jsp的过程中,我发现了一个问题,我这个eclipse由于缺少J ...
- 准备NOIP2017 最长公共子序列(模版)
一些概念: (1)子序列: 一个序列A = a1,a2,--an,中任意删除若干项,剩余的序列叫做A的一个子序列.也可以认为是从序列A按原顺序保留任意若干项得到的序列.例如: 对序列 1,3,5, ...
- swift 集合类型(一)
基本的数组结构Array: var shoppingList: String[] = ["Eggs", "Milk"] 这个shoppingList和传统意义上 ...
- AlwaysOn Group Listener
1.Listener是什么 Listener实际上是一个 VirtualNetworkName,客户端通过这个VNN来连接的具体的sqlserver实例 .Listener包含了DNS名称,port和 ...
- String PK StringBuilder,传说就是传说,只有动手实验,才能得出确定的答案
本机测试结果如下: 大部分情况下,string 性能并不比StringBuilder差,只有特殊情况才出现差异,并非 如前面有些朋友测试的结果哪样,只要使用StringBuilder 就一定比Stri ...
- elk-redis
yum install redis -y vim /etc/redis [root@linux-node1 etc]# grep '^[a-z]' /etc/redis.conf daemonize ...
- 程序开发使用docker部署
我们公司自己研发了一套 grand-line 系统,使用 docker 来部署项目. 我是第一批小白鼠,一开始网络差,build 一次要半个小时,连接进入 web shell 也很慢,部署一个微信项目 ...
- ASP.NET - SqlSugar ORM框架 更新列表
以后SqlSugar所有更新都会在这个贴子更新 SqlSugar是一款轻量级的MSSQL ORM ,除了具有媲美ADO的性能外还具有和EF相似简单易用的语法. 学习列表 0.功能更新 1.SqlSug ...