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
1 class Solution(object): 2 def verifyPreorder(self, preorder): 3 """ 4 :type preorder: List[int] 5 :rtype: bool 6 """ 7 stack = [] 8 min = -0x7FFFFFFF 9 for elem in preorder: 10 if elem < min: 11 return False 12 while stack and stack[-1] < elem: 13 min = stack.pop() 14 stack.append(elem) 15 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。
转载于:https://www.cnblogs.com/lettuan/p/6247229.html
Leetcode 255. Verify Preorder Sequence in Binary Search Tree相关推荐
- LeetCode: 109. Convert Sorted List to Binary Search Tree
题目 Given a singly linked list where elements are sorted in ascending order, convert it to a height b ...
- 【链表递归构造二叉树】LeetCode 109. Convert Sorted List to Binary Search Tree
LeetCode 109. Convert Sorted List to Binary Search Tree Solution1:我的答案 偷鸡摸狗的做法 /*** Definition for s ...
- 【数组递归构造二叉树】LeetCode 108. Convert Sorted Array to Binary Search Tree
LeetCode 108. Convert Sorted Array to Binary Search Tree Solution1:我的答案 构造二叉树利用递归 /*** Definition fo ...
- leetcode -day19 Convert Sorted List to Binary Search Tree
1. Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted ...
- leetcode 108. Convert Sorted Array to Binary Search Tree | 108. 将有序数组转换为二叉搜索树(Java)
题目 https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题解 经典二分,不解释了,直接看代码 /*** ...
- LeetCode: 108. Convert Sorted Array to Binary Search Tree
题目 Given an array where elements are sorted in ascending order, convert it to a height balanced BST. ...
- LeetCode OJ - Convert Sorted List to Binary Search Tree
题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height ...
- LeetCode 108. Convert Sorted Array to Binary Search Tree
题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...
- LeetCode Lowest Common Ancestor of a Binary Search Tree (LCA最近公共祖先)
题意: 给一棵二叉排序树,找p和q的LCA. 思路: 给的是BST(无相同节点),那么每个节点肯定大于左子树中的最大,小于右子树种的最小.根据这个特性,找LCA就简单多了. 分三种情况: (1)p和q ...
最新文章
- UA OPTI570 量子力学33 Time-dependent Perturbation基础
- 众望所归的《JAVASCRIPT凌厉开发--EXT详解与实践 》终于上市了!
- VB6.0连接MySQL数据库
- mysql aes_MYSQL AES加密与解密函数使用
- Linux系统编程---14(回收子线程,回收多个子线程,线程分离,杀死线程)
- 用线程实现动态改变图标
- KD_Tree 算法
- 微波浅谈(一)更新中
- 采用nettcp绑定的wcf宿主到iis7
- DllRegisterServer调用失败
- 摘掉“爆雷”“二房东”的帽子,长租公寓有了新的生存法则
- echarts(五)高级篇(多坐标轴、复杂多坐标轴)
- 【金融项目】尚融宝项目(十一)
- iphone 如何运行android,iPhone成功运行Android,厉害~
- 近年来出现的一种新型AIE结构单元QM-β-Gal发色团-喹啉-丙二腈
- 【智能无线小车系列四】树莓派操作系统安装
- 回滚保存点后究竟发生了什么?
- [Burp Suite完整教程] Intruder Attack typePayloads – 拥有上千种姿态的攻击模式
- 手机地图导航测试用例
- HDU 1079 Calendar Game (博弈)
热门文章
- 【问链-EOS公开课】第十三课 EOS插件机制深入解析
- matlab 涡轮流量计_秦皇岛卫生级流量计定制
- python父类的类成员怎么定义_python如何找到哪些父类定义子对象的方法
- html4.0编辑器,KindEditor HTML(在线编辑器)
- [CH Round #61] 取数游戏
- ICCV 2017 《Unsupervised Learning from Video to Detect Foreground Objects in Single Images》论文笔记
- linux下生成guid,在Linux下生成GUID的程序,及编译错误 “uuid/uuid.h: No such file or directory” 的解决办法。...
- java限频_单个用户及Ip请求频率限制思路(附java实现)
- comsol移动网格_将动网格模型迁移到 5.3a
- php tsrmg,php garbage collect