Verify Preorder/Inorder/Postorder Sequence in Binary Search Tree
Verify Preorder Sequence in Binary Search Tree
\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?
分析:http://my.oschina.net/u/922297/blog/498356
先复习一下BST,给定一个节点,其左子树的所有节点都小于该节点,右子树的所有节点都大于该节点;preorder序列是指在遍历该BST的时候,先记录根节点,再遍历左子树,然后遍历右子树;所以一个preorder序列有这样一个特点,左子树的序列必定都在右子树的序列之前;并且左子树的序列必定都小于根节点,右子树的序列都大于根节点;
根据上面的特点很容易通过递归的方式完成:
如果序列只有一个或者两个元素,那么肯定是正确的;
如果多于两个个元素,以当前节点为根节点;并从当前节点向后遍历,直到大于根节点的节点出现(或者到尾巴),那么根节点之后,大于根节点的节点之前的,是左子树;大于根节点的节点及之后的组成右子树;递归判断左右子树即可;
那么什么时候一个序列肯定不是一个preorder序列呢?前面得到的右子树,如果在其中出现了比根节点还小的数,那么就可以直接返回false了。
public boolean verifyPreorder(int[] seq, int start, int end) {
if (start + >= end)
return true; int root = seq[start];
int i = start + ;
while (i <= end && seq[i] < root) {
i++;
} if (i < end) {
int j = i;
while (j <= end && seq[j] > root) {
j++;
}
if (j < end) {
return false;
} return verifyPreorder(seq, start + , i - ) && verifyPreorder(seq, i, end);
} else {
return verifyPreorder(seq, start + , end);
}
}
Verify Inorder Sequence in Binary Search Tree
判断array是否递增。
Verify Postorder Sequence in Binary Search Tree
判断postorder和上面判断preorder是一模一样的,最后一个是root,然后从头到尾扫,如果当前的值大于root,则判断左边和右边部分是否是BST, 并且判断右边所有的值都大于root。
public boolean verifyPostorder(int[] preorder) {
return verifyPostorder(preorder, , preorder.length - );
} public boolean verifyPostorder(int[] seq, int start, int end) {
if (start + >= end)
return true; int root = seq[end];
int i = start;
while (i <= end - && seq[i] < root) {
i++;
}
if (i < end - ) {
int j = i;
while (j <= end - && seq[j] > root) {
j++;
}
if (j < end - ) {
return false;
} return verifyPostorder(seq, start, i - ) && verifyPostorder(seq, i, end - );
} else {
return verifyPostorder(seq, start, end - );
}
}
Verify Preorder/Inorder/Postorder Sequence in Binary Search Tree的更多相关文章
- [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 255. Verify Preorder Sequence in Binary Search Tree
验证一个list是不是一个BST的preorder traversal sequence. Given an array of numbers, verify whether it is the co ...
- [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 ...
- 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 ...
- [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 ...
- [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 ...
- [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 ...
- 第33题:LeetCode255 Verify Preorder Sequence in Binary Search Tree 验证先序遍历是否符合二叉搜索树
题目 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 考点 1.BST 二叉搜索树 2.递归 思路 1.后序 ...
随机推荐
- AOP PostSharp
using System; using System.Collections.Generic; using System.Linq; using System.Text; using PostShar ...
- Zabbix 3.0 安装笔记
Zabbix 3.0 只支持CentOS 7.0以上版本,所以先在虚拟机中安装好CentOS 7.0 x64,并设置好IP,允许虚拟机联网. 1.安装MySQL 从最新版本的linux系统开始,默认的 ...
- yii2.0自动登录功能的实现方法
参考地址:http://www.kuitao8.com/20150518/3747.shtml 自动登录的原理很简单.主要就是利用cookie来实现的在第一次登录的时候,如果登录成功并且选中了下次自动 ...
- 1.Redis安装(转)
Redis的官网为: http://redis.io/. 1.Redis安装 redis的安装非常的简单,而且Redis并不依赖其他环境和标准库,很容易上手,这可能也是它流行的一个原因.这里为了测试方 ...
- bestcoder Round #7 前三题题解
BestCoder Round #7 Start Time : 2014-08-31 19:00:00 End Time : 2014-08-31 21:00:00Contest Type : ...
- 有关html5设计那些事,你真的考虑过前端的实现吗(最近别人经常问我这种问题,所以我就写一篇了,可能也有别人和我一样吐槽过)
很久以前在安卓2.0系统刚刚的时候就对HTML5比较关注!因为我也是那个时候刚刚入行做前端的.那个时候最大的乐趣就是看着w3plus上面各种css3的效果,觉得哇,好牛逼原来可以这样做,然后3年过去了 ...
- Linux中postfix邮件服务器的搭建
postfix是Wietse Venema在IBM的GPL协议之下开发的MTA(邮件传输代理)软件.postfix是Wietse Venema想要为使用最广泛的sendmail提供替代品的一个尝试.在 ...
- Linux C select函数详解
select IO复用机制: http://www.cnblogs.com/hjslovewcl/archive/2011/03/16/2314330.html http://blog.csdn.ne ...
- Angular.js入门教程
简单介绍 AngularJS是为了克服HTML在构建应用上的不足而设计的.首先Html是一门很好的为静态文本展示设计的声明式语言,但要构建WEB应用的话它就显得乏力了. 通常,我们可以通过以下技术来解 ...
- [转]CentOS 5.3通过yum升级php到最新版本的方法
来自:www.jasonlitka.com/media 通过测试,方法三可行: 方法三 vim /etc/yum.repos.d/utterramblings.repo 输入 [utterrambli ...