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序列有这样一个特点,左子树的序列必定都在右子树的序列之前;并且左子树的序列必定都小于根节点,右子树的序列都大于根节点;

根据上面的特点很容易通过递归的方式完成:

  1. 如果序列只有一个或者两个元素,那么肯定是正确的;

  2. 如果多于两个个元素,以当前节点为根节点;并从当前节点向后遍历,直到大于根节点的节点出现(或者到尾巴),那么根节点之后,大于根节点的节点之前的,是左子树;大于根节点的节点及之后的组成右子树;递归判断左右子树即可;

  3. 那么什么时候一个序列肯定不是一个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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. [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 ...

  4. LeetCode Verify Preorder Sequence in Binary Search Tree

    原题链接在这里:https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree/ 题目: Given an a ...

  5. 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 ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. 第33题:LeetCode255 Verify Preorder Sequence in Binary Search Tree 验证先序遍历是否符合二叉搜索树

    题目 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 考点 1.BST 二叉搜索树 2.递归 思路 1.后序 ...

随机推荐

  1. Yii2 redis与cache

    原文地址:http://www.myexception.cn/php/1974979.html composer require yiisoft/yii2-redis 安装后使用超简单,打开 comm ...

  2. WinForm使用皮肤图文步骤

    Winfrom本身样式提供的是Windows经典样式.. 不说多丑也绝称不上好看..有时为了用户体验就不得不需要想办法弄漂亮一点..皮肤包会是一个不错的选择.. 不废话了..开整.. 首先从网上下载免 ...

  3. Redis3.0.7 cluster/集群 安装配置教程

    1.前言 环境:CentOS-6.7-i386-LiveDVD 安装的CentOs系统 节点: 6个节点,3个主节点.3个从节点(由于redis默认需要3个主节点,如果想每个主节点有一个从节点,这是最 ...

  4. 关于MySQL的SLEEP(N)函数

    都知道通过在MySQL中执行select sleep(N)可以让此语句运行N秒钟: ? 1 2 3 4 5 6 7 mysql> select sleep(1); +----------+ | ...

  5. Linux下,telnet命令如何退出

    测试连接本地的memcached telnet 链接后是这样的: wangkongming@Vostro ~ $ telnet Trying 127.0.0.1... Connected to 127 ...

  6. \r,\n,\r\n的区别

    http://www.studyofnet.com/news/285.html \n是换行,英文是New line,表示使光标到行首\r是回车,英文是Carriage return,表示使光标下移一格 ...

  7. jQuery.imgLazyLoad图片懒加载组件

    一.前言 当一个页面中请求的图片过多,而且图片太大,页面访问的速度是非常慢的,对用户的体验非常不友好:使用图片懒加载,可以减轻服务器的压力,增加页面的访问量,这里主要是总结一下我自己写的图片懒加载组件 ...

  8. Nginx如何隐藏index.html

    我要隐藏目录下的index.html,修改Nginux配置如下: 1.修改文档顺序 index  index.html index.php 2.开启目录流量 在server或location 段里添加 ...

  9. 关于Tchar

    因为C++支持两种字符串,即常规的ANSI编码(使用""包裹)和Unicode编码(使用L""包裹),这样对应的就有了两套字符串处理函数,比如:strlen和w ...

  10. PHP基础之 错误处理 及 异常处理

    错误处理: 1.使用die()方法,结束语句的执行,并输出错误消息 2.自定义错误和错误触发器 自定义错误处理函数(系统有默认的错误处理函数,自定义的错误处理会覆盖默认的处理函数) ========= ...