Leetcode 255. Verify Preorder Sequence in Binary Search Tree
验证一个list是不是一个BST的preorder traversal sequence。

Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree.
You may assume each number in the sequence is unique.
Follow up:
Could you do it using only constant space complexity?
观察这个例子: [8, 3, 1, 6, 4, 7, 10, 14, 13],
8, 3, 1 这三个连续递减的数说明从root开始我们一直在往左边走,直到出现6。
6应该是3的right child,因为他比3大,但是比8小。这时min这个指标应该等于3。也就是说之后list中的所有数都不能小于3。因为6是3的right child,说明3的本身和他的左子树已经遍历完毕,之后的所有数都应该比3大。
然后又是下降,直到出现7。这时应该将min更新为6。之后所有的数都应该大于6。以此类推。我们要做的是维护一个stack。如果elem < min, return False。如果elem < stack[-1], 把elem push进stack。如果elem > stack[-1], 那么pop出stack中比elem小的那些数字,并更新min。具体的步骤看:
min = -MaxInt
8,
8, 3,
8, 3, 1
8, 6 min = 3
8, 6, 4,
8, 7 min = 6
10, min = 8
14, min = 10
14, 13
class Solution(object):
def verifyPreorder(self, preorder):
"""
:type preorder: List[int]
:rtype: bool
"""
stack = []
min = -0x7FFFFFFF
for elem in preorder:
if elem < min:
return False
while stack and stack[-1] < elem:
min = stack.pop()
stack.append(elem)
return True
Follow Up: 如何验证postorder和midorder?
A: 中序排列是递增数列。
postorder的顺序是left-right-root,那么这个例子应该是[1, 4, 7, 6, 3, 13, 14, 10, 8]
比较容易的方式是从root开始验证,由于后续root再后面,我们可以先把list reverse一下。然后思路和上面差不多,只不过要递减改为递增。记录min改成记录max。
Leetcode 255. Verify Preorder Sequence in Binary Search Tree的更多相关文章
- [LeetCode] 255. Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] 255. Verify Preorder Sequence in Binary Search Tree_Medium tag: Preorder Traversal, tree
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- 255. Verify Preorder Sequence in Binary Search Tree
题目: Given an array of numbers, verify whether it is the correct preorder traversal sequence of a bin ...
- [LC] 255. Verify Preorder Sequence in Binary Search Tree
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [Locked] Verify Preorder Sequence in Binary Search Tree
Verify Preorder Sequence in Binary Search Tree Given an array of numbers, verify whether it is the c ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- LeetCode Verify Preorder Sequence in Binary Search Tree
原题链接在这里:https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree/ 题目: Given an a ...
- [Swift]LeetCode255.验证二叉搜索树的先序序列 $ Verify Preorder Sequence in Binary Search Tree
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- 第33题:LeetCode255 Verify Preorder Sequence in Binary Search Tree 验证先序遍历是否符合二叉搜索树
题目 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 考点 1.BST 二叉搜索树 2.递归 思路 1.后序 ...
随机推荐
- Feathers UI 性能优化
Feathers UI作者写的 http://joshblog.net/2013/feathers-performance-tips/
- git操作命令
参考:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 git 分布式版本控制系统. ...
- Oracle system identifier(SID) "xxx" alread exits. Specify another SID
案例环境: 操作系统 :Oracle Linux Server release 5.7 64 bit 数据库版本:Oracle Database 10g Release 10.2.0.4.0 - ...
- ORA-10635: Invalid segment or tablespace type
上周星期天在迁移数据时,碰到了ORA-10635: Invalid segment or tablespace type 错误,当时的操作环境如下: 操作系统版本: [oracle@xxxxx scr ...
- SQL SERVER 2008:内部查询处理器错误: 查询处理器在执行过程中遇到意外错误
今天一个同事突然告诉我,以前跑得很正常的一个SQL语句,执行时突然报如下错误: 消息1222,级别16,状态18,第1 行 已超过了锁请求超时时段. ...
- 【转】高效Java编程工具集锦
原文地址:http://geek.csdn.net/news/detail/57469 Java 开发者常常都会想办法如何更快地编写 Java 代码,让编程变得更加轻松.目前,市面上涌现出越来越多的高 ...
- DMZ区
DMZ是英文“Demilitarized Zone”的缩写,它是为了解决安装防火墙后外部网络不能访问内部网络服务器的问题,而设立的一个非安全系统与安全系统之间的缓冲区,这个缓冲区位于企业内部网络和外部 ...
- gcc中__builtin_return_address和__VA_ARGS__
— Built-in Function: void * __builtin_return_address (unsigned int level) This function returns the ...
- redis、memcache、mongoDB 做了对比
from: http://yang.u85.us/memcache_redis_mongodb.pdf 从以下几个维度,对redis.memcache.mongoDB 做了对比. 1.性能 都比较 ...
- 程序员的修养 -- 如何写日志(logging)
在程序中写日志是一件非常重要,但是很容易被开发人员忽视的地方.写好程序的日志可以帮助我们大大减轻后期维护压力. 在实际的工作中,开发人员往往迫于的巨大时间压力,而写日志又是一个非常繁琐的事情,往往 ...