原题链接在这里:https://leetcode.com/problems/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?

题解:

Method 1 利用stack模拟preorder. stack中放的是左子树. 遇到比stack top还要大, 说明遇到了以top 为root的右子树,把左子树连 & root 都pop出来.

low是当前的lower bound, pop出来说明左边扫完了,不可能出现更小的了,所以更新low. 若是遇到比low还小就说明不是preorder.

Time Complexity: O(n). Space: O(logn).

Method 2 是在array本身上实现stack的功能.

index is initialized as -1, and assign the top of stack as preoder[++index]. But not initialize index as 0, and assignthe top of stack as preorder[index++].

Since every time, the top of stack is preorder[index]. If use index++, it is not the top of stack. Also there is chance of OutOfIndexException.

Time Complexity: O(n). Space: O(1).

AC Java:

 public class Solution {
public boolean verifyPreorder(int[] preorder) {
/*
//Method 1
int low = Integer.MIN_VALUE;
Stack<Integer> stk = new Stack<Integer>();
for(int num : preorder){
if(num < low){
return false;
}
while(!stk.isEmpty() && stk.peek() < num){
low = stk.pop();
}
stk.push(num);
}
return true;
*/
int index = -1;
int low = Integer.MIN_VALUE;
for(int num : preorder){
if(num < low){
return false;
}
while(index >= 0 && preorder[index] < num){
low = preorder[index--];
}
preorder[++index] = num;
}
return true;
}
}

LeetCode Verify Preorder Sequence in Binary Search Tree的更多相关文章

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

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

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

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

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

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

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

  9. [LeetCode] 255. Verify Preorder Sequence in Binary Search Tree_Medium tag: Preorder Traversal, tree

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

随机推荐

  1. PHP上传文件示例

    虽然大多数人认为Web只包含网页,但HTTP协议实际上可以传输任何文件,如office文档.PDF.可执行文件.AVI.压缩文件及各种其他文件类型.虽然FTP在历史上一直是向服务器上传文件的标准方式, ...

  2. 计算机网络中的帧封装(C实现)

    这段时间开始复习计算机网络,看到帧封装这一节,结合以前的课程设计,就用C写了个帧封装的程序,说实话C学的确实不怎么样,实现的时候对于文件操作那部分查了好多资料,下面说说帧封装是啥情况. 学过计算机网络 ...

  3. 从Eclipse迁移到Android Studio碰到的问题记录

    背景: 1. 一直在做.NET的开发,工作之余,学习了一下Android开发,写了一些Demo,当时用的Eclipse开发工具:这两天,刚好项目不是很忙,就打算把之前写的Demo,迁移到Android ...

  4. Android之EACCES (Permission denied)与Permission denied异常探密

    话说,Accipiter君,最近又开始怒学Android了,记得刚开始还是09年学的,现在的手机还是华为出的最早的一款Android手机C8500,那时候就想好好学习Android,赚点小钱,可是~~ ...

  5. 无废话ExtJs 入门教程二十一[继承:Extend]

    无废话ExtJs 入门教程二十一[继承:Extend] extjs技术交流,欢迎加群(201926085) 在开发中,我们在使用视图组件时,经常要设置宽度,高度,标题等属性.而这些属性可以通过“继承” ...

  6. 使用DotNetBar制作漂亮的WinFrom界面,自定义AgileEAS.NET SOA平台WinClient主界面

    一.前言 AgileEAS.NET SOA 中间件平台是一款基于基于敏捷并行开发思想和Microsoft .Net构件(组件)开发技术而构建的一个快速开发应用平台.用于帮助中小型软件企业建立一条适合市 ...

  7. ThinkPHP的D方法和M方法的区别

    M方法和D方法的区别 ThinkPHP 中M方法和D方法都用于实例化一个模型类,M方法 用于高效实例化一个基础模型类,而 D方法 用于实例化一个用户定义模型类. 使用M方法 如果是如下情况,请考虑使用 ...

  8. (转)Mybatis高级映射、动态SQL及获得自增主键

    原文:http://www.cnblogs.com/edwinchen/p/4105278.html?utm_source=tuicool&utm_medium=referral 一.动态SQ ...

  9. [工作中的设计模式]组合模式compnent

    一.模式解析 将对象组合成树形结构以表示“部分整体”的层次结构.组合模式使得用户对单个对象和使用具有一致性. 组合模式的要点是: 1.对象整体以树形层次结构进行展示 2.树的根节点和子节点均可以添加删 ...

  10. iOS 两行代码解决数据持久化

    在实际的iOS开发中,有些时候涉及到将程序的状态保存下来,以便下一次恢复,或者是记录用户的一些喜好和用户的登录信息等等. 这就需要涉及到数据的持久化了,所谓数据持久化就是数据的本地保存,将数据从内存中 ...