zoukankan      html  css  js  c++  java
  • [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 search tree.

    You may assume each number in the sequence is unique.

    Consider the following binary search tree: 

         5
        / 
       2   6
      / 
     1   3
    class Solution {
        public boolean verifyPreorder(int[] preorder) {
            if (preorder == null) {
                return false;
            }
            // keep a decresing stack
            LinkedList<Integer> stack = new LinkedList<>();
            int min = Integer.MIN_VALUE;
            for (int node: preorder) {
                if (node < min) {
                    return false;
                }
                // get into right subtree
                while (!stack.isEmpty() && node > stack.peekFirst()) {
                    min = stack.pollFirst();
                }
                stack.offerFirst(node);
            }
            return true;
        }
    }
  • 相关阅读:
    毕业论文格式
    2018.12.14
    关于百度搜索引擎的优缺点
    2018.12.13
    2018.12.12
    2018.12.11
    2108.12.10
    2018.12.9
    2018.12.8
    2018.12.7
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12213222.html
Copyright © 2011-2022 走看看