Preorder Traversal and BST
Submissions: 7368   Accuracy:

36.71%

  Difficulty: Medium   Marks: 4

Given an array arr of size n, write a program that prints 1 if given array can represent preorder traversal of a BST, else prints 0.

Input:
The first line of input contains an integer T denoting the number of test cases. T testcases follow. Each testcase contains two lines of input.
The first line of each test case is n, denoting the size of array.
The second line of each test case contains n elements of the array.

Output:
For each testcase, in a new line, print 1 if the array represents preorder traversal else print 0.

Constraints:
1 <= T<= 100
1 <= n <= 103
1 <= arr[i] <= 103

Example:
Input:
3
5
40 30 35 80 100
8
40 30 32 35 80 90 100 120
8
7  9 6 1 4 2 3 40

Output:
1
1
0

** For More Input/Output Examples Use 'Expected Output' option **

Author: kartik


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.