Is given level order a BST?

Given an distinct integer array A of size N. You have to check whether the given array can represent the level order traversal of a Binary Search Tree or not.

Input Format:

    First and only argument of input is an integer array A

Output Format:

    return 1 if A can represent level order traversal of a BST, 0 otherwise.

Constraints:

    1 <= N <= 100000
    1 <= A[i] <= 10^9
    All integer in array are distinct

Example:

Input 1:
    A = [7, 4, 12, 3, 6, 8, 1, 5, 10]
Output 1:
    1
Explanation 1:
    Following Bst have same level order.
         7        
        / \       
      4    12      
     / \   /     
    3   6 8    
   /   /   \
  1   5    10
Input 2:
    A = [ 2, 3, 4, 1]
Output 2:
    0
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 Is given level order a BST? on Interview Code Editor
Sign Up
to access hints and editorial solutions for Is given level order a BST?

Discussion


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