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.

Consider the following binary search tree:

     5
/ \
2 6
/ \
1 3
class Solution {
public boolean verifyPreorder(int[] preorder) {
if (preorder == null) {
return false;
}
// keep a decresing stack
LinkedList<Integer> stack = new LinkedList<>();
int min = Integer.MIN_VALUE;
for (int node: preorder) {
if (node < min) {
return false;
}
// get into right subtree
while (!stack.isEmpty() && node > stack.peekFirst()) {
min = stack.pollFirst();
}
stack.offerFirst(node);
}
return true;
}
}

[LC] 255. Verify Preorder Sequence in Binary Search Tree的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. LeetCode Verify Preorder Sequence in Binary Search Tree

    原题链接在这里:https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree/ 题目: Given an a ...

  8. [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 ...

  9. 第33题:LeetCode255 Verify Preorder Sequence in Binary Search Tree 验证先序遍历是否符合二叉搜索树

    题目 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 考点 1.BST 二叉搜索树 2.递归 思路 1.后序 ...

随机推荐

  1. SpringCloud学习之手把手教你用IDEA搭建入门项目(三)

    本篇博客是承接上一篇<手把手教你用IDEA搭建SpringCloud入门项目(二)>,不清楚的请到我的博客空间查看后再看本篇博客,上面两篇博客成功创建了一个简单的SpringCloud项目 ...

  2. Centos6.5 安装zabbix3(收藏,非原创)

    1.安装PHP Zabbix 3.0对PHP的要求最低为5.4,而CentOS6默认为5.3.3,完全不满足要求,故需要利用第三方源,将PHP升级到5.4以上,注意,不支持PHP7 rpm -ivh  ...

  3. git本地仓库连接同步修改远程仓库

    如何使用GIT BASH同步远程仓库 1. 新建一个目录 2. 右键GIT BASH 3. git clone git@github.com:purity12138/221701117.git (SS ...

  4. shell字符串大小写转换

    1.typeset  有两个选项 -l 代表小写 -u 代表大写. 用法: typeset -u name name='asdasdas' echo $name   typeset -l ame am ...

  5. 牛牛的DRB迷宫(DP、二进制编码器)

    牛牛的DRB迷宫I 链接:https://ac.nowcoder.com/acm/contest/3004/A来源:牛客网 题目描述 牛牛有一个n*m的迷宫,对于迷宫中的每个格子都为'R','D',' ...

  6. JOIN US | 京东云诚聘技术精英

    清新的办公区域感受自然的气息,温馨的团队为你我放飞青春的理想 上有天文下有地理的阅读区域 各类图书提供借阅 绿植环绕生机勃勃的会客区域洋溢青春 [高级Java工程师] 职位描述: 参与云计算相关平台/ ...

  7. 超级详细通信协议解析webservice和dubbo通信协议区别

    简单说下接触webservice的背景吧,因为之前的接口对接更多的是成熟的接口品牌像是阿里巴巴.腾讯.聚合数据等,他们接口规范一般都是基于restful进行接口对接.什么是restful接口,可以通过 ...

  8. MJJ玩磁铁

    题目: Problem D: MJJ玩磁铁 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 139  Solved: 9[Submit][Status][ ...

  9. 用一行Python进行数据收集探索

    简易的Pandas之路 任何使用Python数据的人都会熟悉Pandas包.Pandas是大多数行和列格式数据的go-to包.如果你没有Pandas,请确保在终端中使用pip install安装: p ...

  10. 成为优秀Angular开发者所需要学习的19件事

    一款to-do app基本等同于前端开发的"Hello world".虽然涵盖了创建应用程序的CRUD方面,但它通常只涉及那些框架或库也能做到的皮毛而已. Angular看起来似乎 ...