题目:

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://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree/

题解:

使用Stack来模拟preorder traversal ->   mid, left, right。主要代码都是参考了Stefan Pochmann的解答。

Time Complexity - O(n), Space Complexity - O(logn)

public class Solution {
public boolean verifyPreorder(int[] preorder) {
int low = Integer.MIN_VALUE;
Stack<Integer> stack = new Stack<>();
for(int i : preorder) {
if(i < low)
return false;
while(!stack.isEmpty() && i > stack.peek())
low = stack.pop();
stack.push(i);
} return true;
}
}

不使用Stack,Space Complexity O(1)的解法, 利用了原数组

public class Solution {
public boolean verifyPreorder(int[] preorder) {
int low = Integer.MIN_VALUE, index = -1;
for(int i : preorder) {
if(i < low)
return false;
while(index >= 0 && i > preorder[index])
low = preorder[index--];
preorder[++index] = i;
} return true;
}
}

Reference:

https://leetcode.com/discuss/51543/java-o-n-and-o-1-extra-space

https://leetcode.com/discuss/52060/72ms-c-solution-using-one-stack-o-n-time-and-space

https://leetcode.com/discuss/65241/ac-python-o-n-time-o-1-extra-space

https://leetcode.com/discuss/68862/my-c-solution-easy-to-understand

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

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

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

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

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

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

  7. LeetCode Verify Preorder Sequence in Binary Search Tree

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

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

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

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

随机推荐

  1. 每日一“酷”之heapq

    作用:heapq模块实现一个适用于Python列表的最小堆排序算法 堆(heap)是一个属性数据结构,其中子节点与父节点是一种有序关系.二叉堆是一种特殊的堆,二叉堆是完全二元树(二叉树)或者是近似完全 ...

  2. .NET中如何使用反序列化JSON字符串/序列化泛型对象toJsonStr

    在进行 .NET Web MVC 框架开发的网站程序的时候,我们都会遇到最关键的问题,数据传输.   .NET MVC 4中的ControllerBase类建议我们用ViewBag动态数据字典形式(t ...

  3. Web开发从零单排之二:在自制电子请帖中添加留言板功能,SAE+PHP+MySql

    在上一篇博客中介绍怎样在SAE平台搭建一个html5的电子请帖网站,收到很多反馈,也有很多人送上婚礼的祝福,十分感谢! web开发从零学起,记录自己学习过程,各种前端大神们可以绕道不要围观啦 大婚将至 ...

  4. 10.30Daily Scrum

    出席人员 任务分配完成情况 明天任务分配 王皓南 研究代码,讨论实现方法及实现的动能 研究代码,学习相应语言,讨论设计思路 申开亮 研究代码,讨论实现方法及实现的动能 研究代码,学习相应语言,讨论设计 ...

  5. OC特有语法:分类category,给NSString增加方法计算字符串中数字的个数

    1:分类的使用场景:想对一个类,扩充一些功能,而又不改变原来类的模型,也不用继承,这时OC中的特有语法:分类可以做到: 当然分类也是一个类,也需要声明和实现,声明在.h文件中,实现在.m文件中,格式如 ...

  6. iOS 23 种设计模式

    设计模式主要分三个类型:创建型.结构型和行为型. 其中创建型有: 一.Singleton,单例模式:保证一个类只有一个实例,并提供一个访问它的全局访问点 二.Abstract Factory,抽象工厂 ...

  7. linux 错误总结

    帝国cms登录后台提示“登录成功”,接着又提示“您还未登录” 把帝国cms文件夹下的/e/data/adminlogin 目录权限不可写导致,请将此目录权限设置为777权限即可解决.就可以正常登录后台 ...

  8. 高质量的javascript代码 -- 深入理解Javascript

    一. 编写高质量的javascript代码基本要点a) 可维护的代码(Writing Maintainable Code)i. 可读(注释)ii. 一致(看上去是同一个人写的)iii. 已记录b) 最 ...

  9. Virtualbox虚拟机设置不完全笔记

    先说说我想实现的东西,我想在虚拟机安装各种开发环境,我个人在学习Node.然后我装了一个Ubuntu Server,所以我又想共享一个windows下的文件夹,这样可以让我在windows下开发,在L ...

  10. pure.css

    注释中address是纠正的意思  等价于correct/*! Pure v0.5.0 Copyright 2014 Yahoo! Inc. All rights reserved. Licensed ...