题目:

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?

链接: http://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree/

题解:

使用Stack来模拟preorder traversal ->   mid, left, right。主要代码都是参考了Stefan Pochmann的解答。

Time Complexity - O(n), Space Complexity - O(logn)

public class Solution {
public boolean verifyPreorder(int[] preorder) {
int low = Integer.MIN_VALUE;
Stack<Integer> stack = new Stack<>();
for(int i : preorder) {
if(i < low)
return false;
while(!stack.isEmpty() && i > stack.peek())
low = stack.pop();
stack.push(i);
} return true;
}
}

不使用Stack,Space Complexity O(1)的解法, 利用了原数组

public class Solution {
public boolean verifyPreorder(int[] preorder) {
int low = Integer.MIN_VALUE, index = -1;
for(int i : preorder) {
if(i < low)
return false;
while(index >= 0 && i > preorder[index])
low = preorder[index--];
preorder[++index] = i;
} return true;
}
}

Reference:

https://leetcode.com/discuss/51543/java-o-n-and-o-1-extra-space

https://leetcode.com/discuss/52060/72ms-c-solution-using-one-stack-o-n-time-and-space

https://leetcode.com/discuss/65241/ac-python-o-n-time-o-1-extra-space

https://leetcode.com/discuss/68862/my-c-solution-easy-to-understand

255. Verify Preorder Sequence in Binary Search Tree的更多相关文章

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

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

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

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

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

  7. LeetCode Verify Preorder Sequence in Binary Search Tree

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

  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 root密码怎么办?

    摘自:<鸟哥的Linux私房菜> 常常有些朋友在配置好了Linux之后,结果root密码给他忘记去!要重新安装吗?不需要的, 你只要以单人维护模式登陆即可更改你的root密码喔!由于lil ...

  2. Codeforces 559A 第六周 O题

    Description Gerald got a very curious hexagon for his birthday. The boy found out that all the angle ...

  3. mapreduce 实现矩阵乘法

    import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs ...

  4. jetty 8.x, 9.x无法加载jstl的PWC6188问题

    参考: cannot load JSTL taglib within embedded Jetty server:http://stackoverflow.com/questions/2151075/ ...

  5. cdev成员结构体file_operations文件操作结构的分析

    struct file_operations{ struct module *owner; // 指向拥有该结构的模块的指针,避免正在操作时被卸载,一般为初始化为THIS_MODULES loff_t ...

  6. Linux流量监控工具 - iftop (最全面的iftop教程)

    在类Unix系统中可以使用top查看系统资源.进程.内存占用等信息.查看网络状态可以使用netstat.nmap等工具.若要查看实时的网络流量,监控TCP/IP连接等,则可以使用iftop. 一.if ...

  7. JPA学习---第八节:使用JPQL语句进行查询

    1.JPQL 语句查询,代码如下: @Test public void query(){ EntityManagerFactory factory = Persistence.createEntity ...

  8. .Net webservice动态调用

    直接贴代码吧 public class PmsService { /// <summary> /// pms接口 /// </summary> /// <param na ...

  9. .net 自然排序方式

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  10. 【转】edittext设置点击链接

    I put some ClickableSpan in EditText, unfortunately, that spans are still not clickable. When I clic ...