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. HTML5 WebAudioAPI简介(一)

    一.常用对象 1.AudioContext对象 AudioContext是一个专门用于音频处理的接口,并且原理是讲AudioContext创建出来的各种节点(AudioNode)相互连接,音频数据流经 ...

  2. FluentNHibernate当数据库设置默认值时,使用插入操作,导致默认值没有写入问题

    需要再映射属性字段增加Not.Insert() Map(x => x.Provrince, "PROVRINCE").Not.Insert(); Map(x => x. ...

  3. UITableViewCell 上的按钮点击事件

     以前做tableViewCell的button点击事件,总是建立一个全局的可变数组,把data放在数组里,点击获取button的tag值,根据tag从数组了里取data. 其实,如果section只 ...

  4. 通常我们使用[NSDate date]方法得到的时间与当前时间不一致,如何解决?

    NSDate *date = [NSDate date];    NSTimeZone *zone = [NSTimeZone systemTimeZone];    NSInteger interv ...

  5. OC文件操作(2)

    NSFileManager 文件管理器完成文件的创建.移动.拷贝等管理操作 1.查询文件和目录  OC中查询路径下的目录主要分为浅度遍历和深度遍历.  浅度遍历  NSFileManager * ma ...

  6. js string操作总结

    var str = "0123456789"; console.log(str.substring(0)); //------------"0123456789" ...

  7. 原型链和new

    http://www.cnblogs.com/objectorl/archive/2010/01/11/Object-instancof-Function-clarification.html 构造器 ...

  8. Iis 日志文件默认路径

    Iis 日志文件默认路径: C:\WINDOWS\system32\LogFiles

  9. display:inline 跟 display:block 跟 display:inline-block区别

    我来说句人话吧.display:inline; 内联元素,简单来说就是在同一行显示.display:block; 块级元素,简单来说就是就是有换行,会换到第二行.display:inline-bloc ...

  10. phpcms栏目调用

    {loop subcat(0,0,0,$siteid) $r} {php $num++} <h3><a href="{$r[url]}">{$r[catna ...