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.后序 ...
随机推荐
- mslookup
Microsoft Windows [版本 6.1.7601]版权所有 (c) 2009 Microsoft Corporation.保留所有权利. C:\Users\Administrator> ...
- Java String.split()注意点
//String[] aa = "aaa|bbb|ccc".split("|");//错误 String[] aa = "aaa|bbb|ccc&qu ...
- Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space解决方法 问题描述 Exception ...
- Entity Framework学习笔记(五)----Linq查询(2)---贪婪加载
请注明转载地址:http://www.cnblogs.com/arhat 在上一章中,我们使用了Linq对Entity Framework进行了一个查询,但是通过学习我们却发现了懒加载给我来的性能上的 ...
- Access
一般系统的实现: 管理系统的分析与设计 --->>数据表的设计创建 --->> 设计“查询”与“宏” --->> 创建窗体与报表 --->>系统注册 启 ...
- Nginx + Tomcat 动静分离实现负载均衡(转)
0.前期准备 使用Debian环境.安装Nginx(默认安装),一个web项目,安装tomcat(默认安装)等. 1.一份Nginx.conf配置文件 # 定义Nginx运行的用户 和 用户组 如果对 ...
- Mysql几种索引类型的区别及适用情况
如大家所知道的,Mysql目前主要有以下几种索引类型:FULLTEXT,HASH,BTREE,RTREE. 那么,这几种索引有什么功能和性能上的不同呢? FULLTEXT 即为全文索引,目前只有MyI ...
- .NET 轻松实现HTML的绝对过滤之SafeHelper
当今网页中经常使用到网页编辑器,因为人们需要在网页中插入图片,视频,样式等html代码内容,这使得网页的信息更加丰富.随之而来的,也给程序开发者带来了不少麻烦,因为提交的html中难免会出现不安全标记 ...
- WinForm员工信息表
先搞一个panel,然后里面放label.
- PHP之mysql_real_escape_string()函数讲解
定义和用法 mysql_real_escape_string() 函数转义 SQL 语句中使用的字符串中的特殊字符. 下列字符受影响: \x00 \n \r \ ' " \x1a 如果成功, ...