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

Example 1:

Input: [5,2,6,1,3]
Output: false

Example 2:

Input: [5,2,1,3,6]
Output: true

Follow up:
Could you do it using only constant space complexity?

这个题目的思路就用C++ easy to understand solution with thought process and detailed explanation, 因为每次看到preorder[i] > preorder[i-1] 表明有一个node的left tree结束了, 要找到那个node, 然后作为lower bound, i后面的元素应该都比lower bound要大.

T: O(n)    S: O(n)

class Solution:

    def verifyPreorder(self, preorder):
stack, low = [], None
for each in preorder:
if low != None and each < low:
return False
while stack and each > stack[-1]:
low = stack.pop()
stack.append(each)
return True

[LeetCode] 255. Verify Preorder Sequence in Binary Search Tree_Medium tag: Preorder Traversal, tree的更多相关文章

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

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

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

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

  5. LeetCode Verify Preorder Sequence in Binary Search Tree

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

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

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

  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. 【linux系列】yum安装报错 no mirrors to try

    执行以下命令去重新生成缓存 yum clean all yum makecache 更换源重新下载repo文件 重新生成缓存

  2. LeetCode——Nim Game

    Description: You are playing the following Nim Game with your friend: There is a heap of stones on t ...

  3. 重新签名IOS .ipa文件 (包含第三方框架和插件)

    本文未经测试,初步看代码流程接近本人想法,留下作记录.Intoduction This code allow you to resign your own ipa assuming that you ...

  4. LeetCode 81 Search in Rotated Sorted Array II(循环有序数组中的查找问题)

    题目链接:https://leetcode.com/problems/search-in-rotated-sorted-array-ii/#/description   姊妹篇:http://www. ...

  5. SSH使用秘钥和别名登陆服务器

    手工配置免密码及别名登陆 第一步:生成秘钥 $ ssh-keygen -t rsa 第二步:上传公钥到目标服务器 $ ssh-copy-id -i ~/.ssh/id_rsa.pub <romt ...

  6. C++语言的学习环境

    一.Mac C++语言的学习环境 1. 1.1.C语言 :终端->bash-vi/vim文本编辑器->GNU->GCC文件编译器->a.out可执行文件 1.2.C++语言:终 ...

  7. Windows Mysql binlog 数据恢复

    show variables like 'log_bin%'; 可以看到Mysql binlog为关闭状态,那我们去更改为开启状态

  8. python获取windows所有com口

    import serial import serial.tools.list_ports port_list = list(serial.tools.list_ports.comports()) po ...

  9. .NET中的类型对象

    .NET中的任何类型,都有对应的一个类型对象.类型对象和类型实例(类型创建的一个对象)不是同一个概念. 类型对象包含类型的静态字段和方法,当类访问静态方法静态字段,实例调用方法时就会去类型对象中查找静 ...

  10. 查询ip

    ifconfig | grep "inet " | grep -v 127.0.0.1