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?
分析:
  举个例子,5 2 1 3 4 7 6 8。前序遍历,访问顺序是:parent --> left --> right;而要保证这是一个二分搜索树,那么这棵树的每个子树节点从小到大顺序是:left < parent < right;我们发现一旦n(i+1)比n(i)大,那么它必定是之前某个parent的right child,则可以将该parent代表子树用right child压缩表示,即right child取代parent及其left child子树成为新的子树根节点。如果之后来了一个新节点值还比被取代的parent值小的话,return false;如果处理完所有的数,则return true。
解法一:
  如果不考虑O(1)空间复杂度这个条件的话,用stack可实现压缩临近节点的步骤;
代码一:
bool preorder(vector<int> nums) {
stack<int> stk;
int curp = INT_MIN;
if(nums.empty())
return true;
stk.push(nums[]);
for(int i = ; i < nums.size(); i++) {
if(nums[i] < curp)
return false;
while(!stk.empty() && stk.top() < nums[i]) {
curp = stk.top();
stk.pop();
}
stk.push(nums[i]);
}
return true;
}
解法二:
  如果考虑O(1)空间复杂度这个条件,用递归其实不能算是O(1)空间复杂度的,我们可以修改解法一,用原数组来实现stack;
 

[Locked] 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 验证二叉搜索树的先序序列

    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

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

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

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

  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. 第33题:LeetCode255 Verify Preorder Sequence in Binary Search Tree 验证先序遍历是否符合二叉搜索树

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

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

随机推荐

  1. 获取Enum的扩张方法。

    public static class EnumExtention { /// <summary> /// 获取枚举的描述信息 /// </summary> /// <t ...

  2. 关于使用iframe标签自适应高度的使用

    在ifrome内设定最小高度,(此方法只适用于页面内切换高度不一.但是会保留最大高度,返回后保持最大高度不再回到最初页面的高度) <iframe id="one4" widt ...

  3. extjs中gridpanel动态显示/隐藏列

    在extjs3中,大家知道用 myGrid.getColumnModel().setHidden(i,true);但到了4.0后,已经没有getColumnModel这个方法了,我们在Ext.pane ...

  4. [转] 使用CSS3 will-change提高页面滚动、动画等渲染性能 ---张鑫旭

    一.先来看一个例子 下面这个例子来自某外文,我这里简单转述下. 视差滚动现在不是挺流行的嘛,然后Chris Ruppel当其使用background-attachment: fixed实现背景图片不随 ...

  5. C#中foreach遍历学习笔记

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  6. 基于Qt QGraphicsView的多点触摸绘图

    本应用于基于QGraphicsView框架,实现多点触摸. 工程仅仅演示了多点触摸绘图,源自我前段时间一款基于Qt的绘图软件. 工程结构: kmp.h 定义了枚举 slide.h/cpp 定义了派生于 ...

  7. Js随机数--网页版的体育彩票选号器

    <script> function Quickpick() { var ball for( ball = 0; ball < 5; ball++) { this[ball] = pa ...

  8. plot的实践。

    from matplotlib import pyplot as plt data = np.loadtxt(r'C:\Users\yinggang\Desktop\1\a.txt') x,y,z = ...

  9. 快速替换图片的组合-AE-样片!

    模板下载网址:http://pan.baidu.com/s/1hqCbErM

  10. GC的前世与今生

    GC的前世与今生 虽然本文是以.net作为目标来讲述GC,但是GC的概念并非才诞生不久.早在1958年,由鼎鼎大名的图林奖得主John McCarthy所实现的Lisp语言就已经提供了GC的功能,这是 ...