Consider ‘ARR’ = [40, 30, 35, 80, 100]. You can see that it is the preorder traversal of the Binary Search Tree shown above.
Thus, we should return true in this case.
The first line of input contains an integer ‘T’ denoting the number of test cases. then ‘T’ test cases follow.
The first line of each test case consists of a single integer ‘N’ representing the size of the list/array ‘ARR’.
The second line of each test case consists of ‘N’ single space-separated integers representing list/ array ‘ARR’.
For each test case, print a single line containing a string ‘True’ if ‘ARR’ can represent Preorder Traversal of a Binary Search Tree, otherwise print ‘False’.
The output of each test case will be printed in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 50
1 <= N <= 10 ^ 4
1 <= ARR[i] <= 10 ^ 9
Where ‘ARR[i]’ is the element of the given array ‘ARR’ at index ‘i’.
Time limit: 1 sec.
For each ‘i’, we can track the closest node for which this ‘i’th node is on the right subtree using stack. After this, we just need to check whether the value of ‘i’th node i.e ARR[i] is greater or smaller than this root value.
Algorithm
We can implement the previous approach without using the stack as follow -:
Algorithm
Hills and Soldier
Hills and Soldier
Next Greater Element II
Equal Arrays
Three Sum
Sort 0s, 1s, 2s