Question

Formatted question description: https://leetcode.ca/all/255.html

 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

 Example 1:

 Input: [5,2,6,1,3]
 Output: false

 Example 2:

 Input: [5,2,1,3,6]
 Output: true

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

 @tag-tree

Algorithm

First set a minimum value low, then traverse the array, if the current value is less than the minimum value low, return false.

For the root node, push it onto the stack, and then traverse back,

If the number encountered is smaller than the element at the top of the stack, it means that it is the point of its left subtree and continues to be pushed onto the stack.

Until the number encountered is greater than the top element of the stack, then it is the value on the right child. You need to find the right subtree of which node,

So update the low value and delete the top element of the stack, and then continue to compare with the next top element,

If it is still greater than, continue to update the low value and delete the top of the stack, until the stack is empty or the current stack top element is greater than the current value, stop, push the current value,

In this way, if it does not return false before traversing the entire array, return true at the end.

Code

Java

import java.util.Stack;

public class Verify_Preorder_Sequence_in_Binary_Search_Tree {

    public static void main(String[] args) {
        Verify_Preorder_Sequence_in_Binary_Search_Tree out = new Verify_Preorder_Sequence_in_Binary_Search_Tree();
        Solution_dfs s = out.new Solution_dfs();

        System.out.println(s.verifyPreorder(new int[]{5,2,6,1,3}));
        System.out.println(s.verifyPreorder(new int[]{5,2,1,3,6}));


        Solution sss = out.new Solution();

        System.out.println(sss.verifyPreorder(new int[]{5,2,6,1,3}));
        System.out.println(sss.verifyPreorder(new int[]{5,2,1,3,6}));
    }


    public class Solution {
        public boolean verifyPreorder(int[] preorder) {
            int lowBound = Integer.MIN_VALUE;
            Stack<Integer> sk = new Stack<>();
            for (int each : preorder) {
                if (each < lowBound) {
                    return false;
                }

                while (!sk.empty() && each > sk.peek()) { // s>peek => right value
                    lowBound = sk.pop();
                }
                sk.push(each);
            }
            return true;
        }
    }


    public class Solution_dfs {
        public boolean verifyPreorder(int[] preorder) {
            return dfs(preorder, 0, preorder.length - 1, Integer.MIN_VALUE, Integer.MAX_VALUE);
        }

        boolean dfs(int[] preorder, int start, int end, int lower, int upper) {
            if (start > end) return true;

            int val = preorder[start];
            if (val <= lower || val >= upper) return false;

            int i = 0;
            for (i = start + 1; i <= end; ++i) {
                if (preorder[i] >= val) break; // @note: i stops at root's first right child, same as above "if (each < lowBound) {"
            }

            return dfs(preorder, start + 1, i - 1, lower, val) && dfs(preorder, i, end, val, upper);
        }

    }
}

All Problems

All Solutions