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.

Follow up: Could you do it using only constant space complexity?

public class Solution {
    public boolean verifyPreorder(int[] preorder) {
        Stack<Integer> stack =new Stack<>();
        int min = Integer.MIN_VALUE;
        for(int val:preorder){
            if( val < min) return false;

            while(!stack.isEmpty() && val > stack.peek()){
                min = stack.pop();
        return true;

results matching ""

    No results matching ""