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 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的更多相关文章
- [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 ...
- 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 ...
- [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 ...
- [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] 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_Medium tag: Preorder Traversal, 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 ...
- [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 ...
- 第33题:LeetCode255 Verify Preorder Sequence in Binary Search Tree 验证先序遍历是否符合二叉搜索树
题目 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 考点 1.BST 二叉搜索树 2.递归 思路 1.后序 ...
随机推荐
- Activity的Launch mode详解 singleTask正解
Activity有四种加载模式:standard(默认), singleTop, singleTask和 singleInstance.以下逐一举例说明他们的区别: standard:Activity ...
- backbone前端基础框架搭建
前端站点名为:site: 前端框架分为:css.js和img,框架的核心在js文件夹下: js中包括collections.models.views.lib和一个app入口js
- DBus通讯
linux下进程间通信的方式主要有Pipe(管道),FIFO(命名管道),信号,共享内存,消息队列,信号灯等,这些方式各有 各得特点,如管道是linux下命令行中常用的,用于父子进程的通信.但是这些通 ...
- WPF 多线程处理(5)
WPF 多线程处理(1) WPF 多线程处理(2) WPF 多线程处理(3) WPF 多线程处理(4) WPF 多线程处理(5) WPF 多线程处理(6) 项目的目录: 以下是FileStroage的 ...
- 解决方法:java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;
hibernate4.3版本 报错: 把实体注解的声明方式修改一下解决,如: 将 @Entity@Table(name=”table_name”)改为@Entity(name=”table_name” ...
- ORACLE EXPDP命令使用详细【转】
本文转自:http://blog.csdn.net/zftang/article/details/6387325 ORACLE EXPDP命令使用详细 相关参数以及导出示例: 1. DIRECTORY ...
- cookieContainer应用
PublicSharedFunctionGetCookiesSetByPage(ByVal strUrl AsString,ByVal cookieToProvide AsString)AsIEnum ...
- Extension Methods
Oftentimes you’ll find yourself using classes you can’t modify. Whether they’re basic data types or ...
- Unity3D脚本中文系列教程(一)
原地址:http://dong2008hong.blog.163.com/blog/static/46968827201403115643431/?suggestedreading&wumii ...
- 分享: 利用Readability解决网页正文提取问题
原文:http://www.cnblogs.com/iamzyf/p/3529740.html 做数据抓取和分析的各位亲们, 有没有遇到下面的难题呢? - 如何从各式各样的网页中提取正文!? 虽然可以 ...