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 ...
随机推荐
- Swap in C C++ C# Java
写一个函数交换两个变量的值. C: 错误的实现: void swap(int i, int j) { int t = i; i = j; j = t; } 因为C语言的函数参数是以值来传递的(pass ...
- RobotFramework自动化测试之脚本编写(一)
接触了上一篇的RF环境搭建及安装,相比大家都会觉得,哇塞,为什么要做这么多,那么复杂?装那么多干什么有什么用?写脚本会不会也很复杂? 其实首次安装的话 会觉得有点蒙,也不知道安装那么多是拿来干什么的, ...
- Android onActivityResult没响应
原因: 1.当MainActivity2的启动模式为SingleTask时, 系统为自动返回setResult(Activity.RESULT_CANCELED). 2.当为MainActivity2 ...
- linux xorddos样本分析1
样本行为 该样本为国庆期间接到的一个应急,发现为今年比较流行的xorddos,遂分析一番. 运行之后,查看进程,可以发现可疑进程ydxrooqtno,以及ppkzkneour. 多次运行发现除了ydx ...
- Android自动化测试 - Robotium之re-sign.jar重签名后安装失败提示Failure [INSTALL_PARSE_FAILED_NO_CERTIFICATES]解决方案
问题:在用re-sign.jar重签名apk文件后,显示重签名成功,但在实际安装过程中确提示:Failure [INSTALL_PARSE_FAILED_NO_CERTIFICATES] 原因:网上查 ...
- Pig基础学习【持续更新中】
*本文参考了Pig官方文档以及已有的一些博客,并加上了自己的一些知识性的理解.目前正在持续更新中.* Pig作为一种处理大规模数据的高级查询语言,底层是转换成MapReduce实现的,可以作为MapR ...
- 惠普披甲过VR寒冬,花费巨资开发VR游戏
2016被称为VR元年,各大公司都在积极推动该领域的研究,其中HTC.0culus.索尼的成绩是最高的,不仅推出了自家研发的头显,而且销量还很可观.惠普在VR领域自然也有所投入,但是并没有比 ...
- 关于过拟合、局部最小值、以及Poor Generalization的思考
Poor Generalization 这可能是实际中遇到的最多问题. 比如FC网络为什么效果比CNN差那么多啊,是不是陷入局部最小值啊?是不是过拟合啊?是不是欠拟合啊? 在操场跑步的时候,又从SVM ...
- 静态属性,直接把iis搞垮掉 Http error 503 Service Unavailable
属性有个好处,可以在get的时候做一些特殊处理,比如返回一个默认值,正是这个特性,吸引我讲静态字段修改了成静态属性,代码如下: public static string 微信订阅号 { get { i ...
- Sqlserver查询结果,让某列结果合并一列并且逗号分隔。
create function [dbo].[mergeName](@Id bigint) returns nvarchar(500) as begin dec ...