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 ...
随机推荐
- Jetty使用教程(四:24-27)—Jetty开发指南
二十四.处理器(Handler ) 24.1 编写一个常用的Handler Jetty的Handler组件用来处理接收到的请求. 很多使用者不需要编写Jetty的Handler ,而是通过使用Serv ...
- 使用Navicat 导入导出Mysql数据库
1 导出 另外一种方式 2 导入,新建数据名称 3 导入,运行sql文件(步骤1中的) 推荐使用SQLyog进行导入数据比较好
- ajax下载文件
得到所有Post数据: var postData=Request.Form.ToString() 构建JS代码 // Ajax 文件下载jQuery.download = function(url, ...
- ubuntu下配置jdk
1.首先下载jdk-7u51-linux-i586.tar.gz.并将它放在例如/home目录. 2.解压安装 sudo tar zxvf ./jdk-7u51-linux-i586.tar.gz ...
- Python学习笔记(二)——HelloWorld
一.交互式化环境下书写代码 二.使用文本编辑器编辑.py文件 1.建议,使用Nodepad++,好看,免费,度娘直接搜素即可. 2.编写代码 3.保存为.py结尾的文件 4.使用cmd,打开到文件所在 ...
- Android ADB命令大全
通过ADB命令查看wifi密码.MAC地址.设备信息.操作文件.查看文件.日志信息.卸载.启动和安装APK等 ADB很强大,记住一些ADB命令有助于提高工作效率. 获取序列号: adb get-s ...
- SQL Server 数据库巡检脚本
--1.查看数据库版本信息 select @@version --2.查看所有数据库名称及大小 exec sp_helpdb --3.查看数据库所在机器的操作系统参数 exec master..xp_ ...
- hud 5876 2016 ACM/ICPC Asia Regional Dalian Online
题意:给一个图 给定一个点s 求补图中s点到达各个点的最短路 思路:从s点开始bfs 在图中与s点有连接的都是在补图中不能直接到达的点 反之在补图中都是可以直接到达的点 由此bfs ((( 诡异的写法 ...
- Javascript初学篇章_1(概念/数据类型)
Javascript是一门脚本语言,主要由浏览器来执行.它可以说是页面的灵魂,让页面活过来.与之前学的HTML5+CSS样式的不同之处就在于,JS能让静态网页成为一个动态网页,实现与用户的互动. Ja ...
- fnc.tld学习编写
使用 el 的过程中,需要使用到后端代码处理逻辑,这个时候我们就需要自定义 方法. 如我们后端代码定义如下: package com.rhythmk.common; public class FncH ...