LeetCode Verify Preorder Sequence in Binary Search Tree
原题链接在这里: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的更多相关文章
- [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 ...
- [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 验证二叉搜索树的先序序列
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 ...
- 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 ...
- [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.后序 ...
- [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 ...
随机推荐
- 关于ActiveMQ的几种集群配置
ActiveMQ的几种集群配置. Queue consumer clusters 此集群让多个消费者同时消费一个队列,若某个消费者出问题无法消费信息,则未消费掉的消息将被发给其他正常的消费者,结构图如 ...
- XSS跨站点脚本攻击
XSS攻击:跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS. 以下为Jav ...
- MyEclipse Project Migration功能中文简单介绍
前端时间,我对myEclispe的project Migration产生了疑问,也不知道是干啥用的.然后百度之,翻译结果是项目迁移,再次百度其他人对这个的经验,没想到百度到的没多少,甚至都没有说明这个 ...
- [Java 基础]sun.misc.Unsafe
使用Unsafe可以干一些好事. Unsafe对象初始化 如果你的代码属于trusted的,可以直接使用下面的方式: public static Unsafe getUnsafe() { Class ...
- 最小化安装centos7不能执行ifconfig命令的解决方法
操作环境 虚拟机:VirtualBox 5.0.4 for Windows 操作系统:CentOS-7-x86_64-Minimal-1503-01.iso 问题描述 登录系统之后,输入ifconfi ...
- Jmeter MySQL数据库性能测试
1.首先准备M一SQL数据,新建一个数据库及测试用的表,插入1条数据 2.打开Jmeter,新建线程组,设置多少用户,循环几次随意 3.在线程组下新增JDBC配置元件,通过配置使得Jmeter能够连上 ...
- 读写注册表 registrykey 创建删除
namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { Initialize ...
- React的双向绑定
以前对于双向绑定概念来自于Angular.js,现在我用我感兴趣的react.js来实现这样的方式.有2种方式分析,1:不用插件,2:用插件 (引入react.js操作省略...) 不用插件: 先创建 ...
- 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest
A. Toda 2 按题意模拟即可. #include <bits/stdc++.h> using namespace std ; typedef pair < int , int ...
- Why do we live in this world?
Why do we live in this world? It seems to me there is nothing but two reasons, - to live the livabil ...