A binary tree is a tree where each node has at most two children. It is characterized by any of the following properties:
It can be an empty tree, where root = null.
It can contain a root node which contain some value and two subtree, left subtree and right subtree, which are also binary tree.
A binary tree is a binary search tree (BST) if all the non-empty nodes follows both two properties:
Each node's left subtree contains only values less than it, and
Each node's right subtree contains only values greater than it.
Preorder traversal is a tree traversal method where the current node is visited first, then the left subtree and then the right subtree. More specifically, let's represent the preorder traversal of a tree by a list. Then this list is constructed in following way:
If the tree is empty, then this list be a null list.
For non-empty tree, let's represent the preorder of left subtree as L and of right subtree as R. Then the preorder of tree is obtained by appending L to current node, and then appending R to it.
Given a list of numbers, determine whether it can represent the preorder traversal of a binary search tree(BST).
The first line contains the number of test cases, T. Then T test cases follow. The first line of each test case contains the number of nodes in the tree, N. In next line there will a list of N unique numbers, where each number is from set [1, N].
For each test case, print "YES" if there's exist a BST whose preorder is equal to the list otherwise "NO" (without quotes).