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?

这道题让给了一个一维数组,让我们验证其是否为一个二叉搜索树的先序遍历出的顺序,二叉搜索树的性质是左<根<右,如果用中序遍历得到的结果就是有序数组,而先序遍历的结果就不是有序数组了,但是难道一点规律都没有了吗,其实规律还是有的,根据二叉搜索树的性质,当前节点的值一定大于其左子树中任何一个节点值,而且其右子树中的任何一个节点值都不能小于当前节点值,可以用这个性质来验证,举个例子,比如下面这棵二叉搜索树:

    / \

  / \
    

其先序遍历的结果是 {5, 2, 1, 3, 6},先设一个最小值 low,然后遍历数组,如果当前值小于这个最小值 low,返回 false,对于根节点,将其压入栈中,然后往后遍历,如果遇到的数字比栈顶元素小,说明是其左子树的点,继续压入栈中,直到遇到的数字比栈顶元素大,那么就是右边的值了,需要找到是哪个节点的右子树,所以更新 low 值并删掉栈顶元素,然后继续和下一个栈顶元素比较,如果还是大于,则继续更新 low 值和删掉栈顶,直到栈为空或者当前栈顶元素大于当前值停止,压入当前值,这样如果遍历完整个数组之前都没有返回 false 的话,最后返回 true 即可,参见代码如下:

解法一:

class Solution {
public:
bool verifyPreorder(vector<int>& preorder) {
int low = INT_MIN;
stack<int> s;
for (auto a : preorder) {
if (a < low) return false;
while (!s.empty() && a > s.top()) {
low = s.top(); s.pop();
}
s.push(a);
}
return true;
}
};

下面这种方法和上面的思路相同,为了使空间复杂度为常量,我们不能使用 stack,所以直接修改 preorder,将 low 值存在 preorder 的特定位置即可,前提是不能影响当前的遍历,参见代码如下:

解法二:

class Solution {
public:
bool verifyPreorder(vector<int>& preorder) {
int low = INT_MIN, i = -;
for (auto a : preorder) {
if (a < low) return false;
while (i >= && a > preorder[i]) {
low = preorder[i--];
}
preorder[++i] = a;
}
return true;
}
};

下面这种方法使用了分治法,跟之前那道验证二叉搜索树的题 Validate Binary Search Tree 的思路很类似,在递归函数中维护一个下界 lower 和上界 upper,那么当前遍历到的节点值必须在 (lower, upper) 区间之内,然后在给定的区间内搜第一个大于当前节点值的点,以此为分界,左右两部分分别调用递归函数,注意左半部分的 upper 更新为当前节点值 val,表明左子树的节点值都必须小于当前节点值,而右半部分的递归的 lower 更新为当前节点值 val,表明右子树的节点值都必须大于当前节点值,如果左右两部分的返回结果均为真,则整体返回真,参见代码如下:

解法三:

class Solution {
public:
bool verifyPreorder(vector<int>& preorder) {
return helper(preorder, , preorder.size() - , INT_MIN, INT_MAX);
}
bool helper(vector<int>& preorder, int start, int end, int lower, int upper) {
if (start > end) return true;
int val = preorder[start], i = ;
if (val <= lower || val >= upper) return false;
for (i = start + ; i <= end; ++i) {
if (preorder[i] >= val) break;
}
return helper(preorder, start + , i - , lower, val) && helper(preorder, i, end, val, upper);
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/255

类似题目:

Binary Tree Preorder Traversal

Validate Binary Search Tree

参考资料:

https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree/

https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree/discuss/68142/Java-O(n)-and-O(1)-extra-space

https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree/discuss/68185/C%2B%2B-easy-to-understand-solution-with-thought-process-and-detailed-explanation

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 255. Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列的更多相关文章

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

  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. [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树

    4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...

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

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

  5. [LeetCode] Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  6. [LeetCode] 98. Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

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

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

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

随机推荐

  1. win7 64bit安装redis

    win7 64bit安装redis 1 先安装redis客户端 1.下载Redis的压缩包 https://github.com/dmajkic/redis/downloads 我下载的是redis- ...

  2. RestController 能不能通过配置关闭

    https://stackoverflow.com/questions/29958231/can-a-spring-boot-restcontroller-be-enabled-disabled-us ...

  3. TEXT_CONVERT_XLS_TO_SAP 错误排查

    转自:https://blog.csdn.net/ityangjia/article/details/88827308 本文链接:https://blog.csdn.net/ityangjia/art ...

  4. C# Large Files MD5 C# 获取大文件MD5

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. vue实现word,pdf文件的导出功能

    vue实现word或pdf文档导出的功能,我的项目是:后端返回一个文档流(下图),然后前端对文档流做处理进行下载,代码如下: import axios from 'axios'; axios.get( ...

  6. js中this关键字的作用

    this中的几种情况 1.普通函数中的this window 2.构造函数中的this 是当前构造函数创建的对象在new这个构造函数的时候会在内存中创建一个对象,此时会让this指向刚创建好的这个对象 ...

  7. 【转】聊一聊-JAVA 泛型中的通配符 T,E,K,V,?

    原文:https://juejin.im/post/5d5789d26fb9a06ad0056bd9 前言 Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型 ...

  8. 利用Python模拟登录pastebin.com

    任务 在https://pastebin.com网站注册一个账号,利用python实现用户的自动登录和创建paste.该任务需要分成如下两步利用python实现: 账号的自动登录 paste的自动创建 ...

  9. jQuery基础的HTML与text区别

    浏览器样式 <body> <h1>jQueryAPI特点<a href="#">a标<i>来个斜体</i>签</a ...

  10. 多线程学习笔记(三) BackgroundWorker 暂停/继续

    BackgroundWorker bw; private ManualResetEvent manualReset = new ManualResetEvent(true); private void ...