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.后序 ...
随机推荐
- Macosx 安装 ionic 成功教程
一.首先介绍一下ionic ionic是一个用来开发混合手机应用的,开源的,免费的代码库.可以优化html.css和js的性能,构建高效的应用程序,而且还可以用于构建Sass和AngularJS的优化 ...
- B样条曲线曲面(附代码)
1 B样条曲线 1.1 B样条曲线方程 B样条方法具有表示与设计自由型曲线曲面的强大功能,是形状数学描述的主流方法之一,另外B样条方法是目前工业产品几何定义国际标准——有理B样条方法 (NURBS)的 ...
- [Erlang 0105] Erlang Resources 小站 2013年1月~6月资讯合集
很多事情要做,一件一件来; Erlang Resources 小站 2013年1月~6月资讯合集,方便检索. 小站地址: http://site.douban.com/204209/ ...
- MySQL备份还原——mysqldump工具介绍
mysqldump是一款MySQL逻辑备份的工具,他将数据库里面的对象(表)导出成SQL脚本文件.有点类似于SQL SEVER的"任务-生成脚本"的逻辑备份功能.mysqldump ...
- ORACLE 11g 数据库体系结构图
ORACLE 11g 的数据库体系结构图,非常全面.系统.高屋建瓴的整体介绍了ORACLE 11g 的数据库体系结构.如果能全面了解.清晰梳理.深入掌握这些知识点,相信对你了解学习.深入研究ORACL ...
- Why Do We Need a Data Warehouse?
https://dwbi1.wordpress.com/2012/12/03/why-do-we-need-a-data-warehouse/ 经常有人来质疑数据仓库的价值,为什么我们需要花费一年多的 ...
- LINUX下的PHP
由于linux系统的稳定性,大部分的PHP服务器都被部署在linux上,而且像redis等扩展在linux能得到更好的支持,所以对于PHP程序员来说,使用linux的功底也相当重要,接下来总结一下我从 ...
- hive建表与数据的导入导出
建表: create EXTERNAL table tabtext(IMSI string,MDN string,MEID string,NAI string,DestinationIP string ...
- Windows Server 2012 虚拟化实战:存储(一)
在计算机世界我们随处可以见的一种方法,那就是抽象.1946年冯诺依曼提出了计算机的基本结构包含:计算器,存储器和I/O设备.这可能是对计算机这一新生事物最重要的一次抽象,它直接影响了今后几十年计算机软 ...
- ipv4理论知识1-ipv4介绍,ipv4记法,地址段个数算法
定义 在TCP/IP协议中,用于在IP层识别连接到因特网设备的标识符称为因特网地址或IP地址.IPv4地址是一个32位的地址. 地址空间 像IPv4这种定义了地址的协议都有一个地址空间.地址空间就是协 ...