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.后序 ...
随机推荐
- LSM存储模型
LSM存储模型 数据库有3种基本的存储引擎: 哈希表,支持增.删.改以及随机读取操作,但不支持顺序扫描,对应的存储系统为key-value存储系统.对于key-value的插入以及查询,哈希表的复杂度 ...
- GoLang之基础
GoLang之基础 Go是一种并发的.带垃圾回收的.快速编译的语言. 经典的"hello world"入门: package main import "fmt" ...
- Nginx IP访问控制,只允许指定的IP地址访问
Nginx可以进行IP访问控制,配置指定的IP地址访问服务器网站 今天领导提出一个新的业务需求,网站上线时让外部用户在上线时间段访问到的页面是维护页面,公司内部员工在上线时段可用正常访问公司的网站. ...
- github常见操作和常见错误!
本人总结: 1.问题: fatal: Not a git repository (or any of the parent directories) 解决: 本地库还没有创建,请先用git init ...
- 网上搜的一个shell中 中文设置的一个样例;
from:http://www.cnblogs.com/52linux/archive/2012/03/24/2415082.html SSH Secure Shell Client中文乱码的解决方法 ...
- 推荐 10 个超棒的 CSS3 代码生成工具
新的在线工具和 WebApp 帮助开发者快速地创建网站而不用写代码.前端开发已经在框架和代码库方面有了很大的进展. 但是许多开发者已经忘记了代码生成器在构建网站时的价值.下面的资源是完全免费的 Web ...
- 自执行的匿名函数!function()
最近有空可以让我静下心来看看各种代码,function与感叹号的频繁出现,让我回想起2个月前我回杭州最后参加团队会议的时候,@西子剑影抛出的一样的问题:如果在function之前加上感叹号 (!) 会 ...
- 【Solr】 solr对拼音搜索和拼音首字母搜索的支持
问:对于拼音和拼音首字母的支持,当你在搜商品的时候,如果想输入拼音和拼音首字母就给出商品的信息,怎么办呢? 实现方式有2种,但是他们其实是对应的. 用lucene实现 1.建索引, 多建一个索引字段 ...
- CodeForces 353B Two Heaps
B. Two Heaps Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily cho ...
- eclipse工具背景色模板-程序员保护好自己的眼睛
做为coder,要保护好自己的眼睛,eclipse 强烈推荐 Eclipse Color Theme插件,该插件包含多种当前流行的主题选择. 安装方法: 安装方法:1.先安装一个Eclipse Col ...