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;
        }
    }
  • 相关阅读:
    1070 结绳
    1069 微博转发抽奖
    1068 万绿丛中一点红
    1067 试密码
    1066 图像过滤
    1065 单身狗
    CSS--文本溢出与换行
    css--滤镜filter
    css--flex布局
    css--table布局
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12213222.html
Copyright © 2011-2022 走看看