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.后序 ...
随机推荐
- Thrift 的原理和使用
thrift 的原理和使用 Thrift 架构 Thrift是一个跨语言的服务部署框架,最初由Facebook于2007年开发,2008年进入Apache开源项目.Thrift通过IDL(Interf ...
- 在Razor中如何引入命名空间?("import namespace in razor view") 【转】
原文链接 找了半天,原来如此: 在aspx中: <%@ Import Namespace = "Martian.Areas.SFC.Models" %><%@ I ...
- php json_decode 函数
json_decode 函数 url地址:http://php.net/manual/en/function.json-decode.php json_decode (PHP 5 >= 5.2. ...
- CF451B Sort the Array 水题
Codeforces Round #258 (Div. 2) Sort the Array B. Sort the Array time limit per test 1 second memory ...
- CF455C Civilization (并查集)
CF456E Codeforces Round #260 (Div. 1) C Codeforces Round #260 (Div. 2) E http://codeforces.com/conte ...
- Junit初级编码(一)第一个Junit测试程序
序,Junit测试是单元测试的一个框架,提供了很多方法,供我们快速开展单元测试.目前最新版本JAR包为4.12,官网地址为http://junit.org/ 一.第一个Junit测试程序 1 去官网下 ...
- 给select添加自定义值和选项
添加选项: document.getElementById("id_select").options.add(new Option("name", " ...
- iOS分类、延展和子类的区别
iOS分类.延展和子类的区别 类别.延展.子类的区别 类别 延展 子类 功能 为类添加方法,不用知道类的源码,添加变量(通过运行时,具体参考下面注解) 为类添加私有变量和私有方法,在类的源文件中书 ...
- AlwaysOn可用性组测试环境安装与配置(二)--AlwaysOn配置(界面与T-SQL)
四.AlwaysOn配置 1.开启AlwaysOn高可用性功能. 1.1.开启Server01的可用性组 1.2.需要重启服务:属于SQL server群集节点的服务,需要通过故障转移界面重启 1.3 ...
- 微信封号浪潮再起 A货假代购还能在朋友圈泛滥多久?
你的微信朋友圈是不是很活跃?是不是被很多所谓的名品所包围?没错,这些很多都是A货或假代购的伎俩.如果xmyanke的微信朋友圈出现这些东东,我就会直接屏蔽他的朋友圈权限.具体方法是:打开他的微信详细资 ...