#C14392. Valid Inorder Traversal of BST
Valid Inorder Traversal of BST
Valid Inorder Traversal of BST
You are given an array of integers which is claimed to be the inorder traversal of a Binary Search Tree (BST). Recall that for any BST, the inorder traversal produces a sorted sequence in strictly increasing order (i.e. all values are unique and arranged in ascending order). Your task is to verify whether the given array satisfies these conditions.
The problem requires you to check if the array is sorted in ascending order and has no duplicate elements. If both conditions hold, then the array can represent the inorder traversal of a valid BST.
Note: An empty array or an array with a single element is always considered valid.
Input Format: The first line contains an integer n, the number of elements in the array. The second line (if n > 0) contains n space-separated integers.
Output Format: Print True
if the array can be the inorder traversal of a BST, and False
otherwise.
The solution must read input from standard input (stdin) and write the result to standard output (stdout).
inputFormat
The first line contains an integer n
indicating the number of elements in the array. The second line contains n
space-separated integers representing the inorder traversal of a binary search tree. If n
is 0, the second line will be absent.
outputFormat
Output a single line with either True
if the input array is a valid inorder traversal of a BST, or False
otherwise.
7
2 3 5 7 11 13 17
True