InterviewBit Academy is now Scaler!
Learn Tech Skills from Scratch @ Scaler EDGE

Valid BST from Preorder


Problem Description

You are given a preorder traversal A, of a Binary Search Tree.

Find if it is a valid preorder traversal of a BST.



Problem Constraints

1 <= A[i] <= 106

1 <= |A| <= 105



Input Format

First and only argument is an integer array A denoting the pre-order traversal.



Output Format

Return an integer:

  • 0 : Impossible preorder traversal of a BST
  • 1 : Possible preorder traversal of a BST


  • Example Input

    Input 1:

    A = [7, 7, 10, 10, 9, 5, 2, 8]
    



    Example Output

    Output 1:

     1



    NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
    Start solving Valid BST from Preorder on Interview Code Editor
    Sign Up
    to access hints and editorial solutions for Valid BST from Preorder
    Asked In:

    Discussion


    Loading...
    Click here to start solving coding interview questions