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. CSS3 box-sizing 属性

    定义和用法 box-sizing 属性允许您以特定的方式定义匹配某个区域的特定元素. 例如,假如您需要并排放置两个带边框的框,可通过将 box-sizing 设置为 "border-box& ...

  2. HTML5 文件域+FileReader 读取文件并上传到服务器(三)

    一.读取文件为blob并上传到服务器 HTML <div class="container"> <!--读取要上传的文件--> <input type ...

  3. Member var and Static var.

    /* member variable and static variable: 1,invoke ways: member variable,also called 'instance' variab ...

  4. [转]关于java中的 sychronized 同步方法 与 同步块的理解

    首先,需要说明一点,也是最重要的一点,无论是同步方法 还是 同步块 都是只针对同一个对象的多线程而言的,只有同一个对象产生的多线程,才会考虑到 同步方法 或者是 同步块,如果定义多个实例的同步,可以考 ...

  5. 怎样在官网上下载xcode7.2

    其实我觉得还是有必要就这个写一篇论文的  以证明自己真的是个菜鸟 首先进入苹果开发者官网 https://developer.apple.com/ 选择 resource 然后 点击加号  然后下载就 ...

  6. 【BZOJ2653】【主席树+二分】middle

    Description 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整. 给你一个长度为n的序列s. 回答Q个这样的询问:s的左端点在[a,b ...

  7. 3 - testng.xml

    TestNG的调用有以下几种方式: testng.xml ant 命令行 这部分主要介绍testng.xml的格式. 当前testng.xml的DTD(文档类型定义(Document Type Def ...

  8. jQuery-弹窗登录

    在jQuery中实现弹窗常要用到的方法有: width()  :元素的宽度 outerWidth()  元素的宽度 盒子的padding+border 总的宽度 scrollTop()  鼠标滚轮自上 ...

  9. JS call和apply用法(转)

    每个JavaScript函数都会有很多附属的(attached)方法,包括toString().call()以及apply().听起来,你是否会 感到奇怪,一个函数可能会有属于它自己的方法,但是记住, ...

  10. html的input输入框提示信息 点击隐藏

    <input type="text"    <!-- 文本框 --> name="textfield" value="请输入...& ...