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.后序 ...
随机推荐
- python之选课系统详解[功能未完善]
作业需求 思路:1.先写出大体的类,比如学校类,学生类,课程类-- 2.写出类里面大概的方法,比如学校类里面有创建讲师.创建班级-- 3.根据下面写出大致的代码,并实现其功能 遇到的困 ...
- .Net缓存管理框架CacheManager
using System; using CacheManager.Core; namespace ConsoleApplication { class Program { static void Ma ...
- WinHEC 2015 深圳之行
在上个月,本人有幸在MVP Team和MVP Lead的帮助下,参加了微软在深圳召开的WinHEC 2015大会. 在本次大会上,见证了微软和国内企业的合作,并且发布一些关于在Windows 10方面 ...
- Linux 磁盘自检介绍
在Linux系统中,有时候重启会耗费非常长的时间,如果你进一步检查细节,就会发现绝大部分时间都耗费在磁盘自检(fsck)上了,有时候遇到时间比较紧急的情况,磁盘自检耗费的时间非常长,真的是让人心焦火急 ...
- W3School-CSS 内边距 (padding) 实例
CSS 内边距 (padding) 实例 CSS 实例 CSS 背景实例 CSS 文本实例 CSS 字体(font)实例 CSS 边框(border)实例 CSS 外边距 (margin) 实例 CS ...
- 烂泥:阿里云RDS本地恢复数据
本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb 公司目前使用的数据库是阿里云的RDS,目前RDS的版本为mysql5.6.如下: 现在要 ...
- Walle - 瓦力上线部署系统搭建攻略
背景知识:Walle - 瓦力是一个支持svn.git.多用户.多项目.多环境同时部署的上线部署系统,http://www.oschina.net/news/68610/walle-0-9-2 实验系 ...
- Nodejs以后台服务启动
1: 从网上查找 LINUX中我们可以使用这种简单的方式让node.js在后台运行: nohup node your_app.js & 经多次实验一直没有成功 2:使用 forever ...
- Excel自文本导入内容时如何做到单元格内换行
前言:今天在处理数据的时候,在数据库中用到了\n换行符号,目的是在同表格内做到数据多行显示,比如 字段名1 字段名2 字段名3 1 数据一行 数据二行 数据三行 例子是在sql查询后的结果 ...
- WPF Prism
简介 图解 参考 Using the Model-View-ViewModel Pattern