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. Nginx之epoll和select poll

    epoll和 select poll 都是做I/O多路复用的. 区别在于: epoll较灵活,如果有一百万个链接状态同时保持,但是在某个时刻,只有几百个链接是活跃的.epoll的处理是通过epoll_ ...

  2. jquery ajax常用的登录登出

    整理jquery+ajax的登录登出方法. //登录 var currentUserId = -1; $(function() { var timestamp = (new Date()).value ...

  3. UVA - 1630 Folding(串折叠)(dp---记忆化搜索)

    题意:给出一个由大写字母组成的长度为n(1<=n<=100)的串,“折叠”成一个尽量短的串.折叠可以嵌套.多解时可输出任意解. 分析: 1.dp[l][r]为l~r区间可折叠成的最短串的长 ...

  4. Cavace 自定义View绘制

    一.开发资料与实例教程1.跟囧猫学之Canvas.Matrix 倒影实例教程 http://www.eoeandroid.com/thread-158506-1-1.html 2.Gridview 控 ...

  5. share团队冲刺10

    团队冲刺第十天 昨天:完善代码,美化界面 今天:整合全部代码,基本完成作品 问题:无

  6. nodejs(14)express获取url中的参数

    问号传参获取参数 获取 http://127.0.0.1:3001/user?id=10&name=zs 中的查询参数: 直接使用 req.query 获取参数即可: 注意:URL 地址栏中通 ...

  7. vivado下创建基本时序周期约束

    创建基本时钟周期约束.(验证我们的设计能否在期望的频率上运行) (学习记录,晚一点会做实验传上来的.) 时钟基本概念:https://blog.csdn.net/wordwarwordwar/arti ...

  8. 吴裕雄--天生自然 JAVASCRIPT开发学习:测试 jQuery

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. linux 下实用工具

    gpm 让linux 纯字符终端具备窗口模式下的鼠标功能 xterm + tmux 支持横向或者纵向切屏的终端 urxvt-unicode 支持中文的终端

  10. [RoarCTF 2019]Easy Calc-协议层攻击之HTTP请求走私

    0X01:什么是HTTP请求走私 HTTP请求走私属于协议层攻击,是服务器漏洞的一种. HTTP请求走私是一种干扰网站处理从一个或多个用户接收的HTTP请求序列的方式的技术.使攻击者可以绕过安全控制, ...