LogiQuest

388. Validate a BST

MediumBSTInorder

Print YES if the binary tree is a valid binary search tree (every left subtree < node < every right subtree), else NO.

Input format

Line 1: n (number of level-order entries) Line 2: the values in level order, -1 for a missing child

Output format

YES or NO

Example 1

Input

3
2 1 3

Output

YES

Example 2

Input

7
5 1 4 -1 -1 3 6

Output

NO

Constraints

1 ≤ nodes ≤ 10⁴ (values are positive)

Read input from standard input and print the answer to standard output. Your code is judged against 6 test cases, most of them hidden.

Loading the MyLoginn IDE…