#C11086. Valid BST Verification
Valid BST Verification
Valid BST Verification
You are given an array that represents a binary tree in level-order. In this representation, a value of -1
indicates a null node. Your task is to determine whether the given binary tree is a valid Binary Search Tree (BST).
A binary tree is a BST if for every node with value \(v\), all values in its left subtree are strictly less than \(v\), and all values in its right subtree are strictly greater than \(v\). Formally, if a node has a value \(v\) and its left and right subtrees have values \(L\) and \(R\) respectively, the following must hold:
[ L < v < R ]
The binary tree is given in a level-order format. You must process multiple test cases. For each test case, output "Yes" if the tree is a valid BST and "No" otherwise.
Note: An empty tree (where the number of nodes is 0) is considered a valid BST.
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains an integer \(T\) representing the number of test cases.
- For each test case:
- The first line contains an integer \(N\) indicating the number of nodes in the tree.
- The second line contains \(N\) space-separated integers representing the tree in level-order. A value of
-1
represents a null node.
outputFormat
For each test case, output a single line to standard output (stdout) containing "Yes" if the tree is a valid BST, or "No" otherwise.
## sample4
7
5 3 8 1 4 6 10
5
10 5 15 -1 -1 6 20
3
2 1 3
3
3 2 1
Yes
No
Yes
No
</p>