-->
当前位置:首页 > 题库

PROGRAMMING:Cartesian Tree

Luz5年前 (2021-05-10)题库438
Cartesian tree is a special binary tree, its node contains two keywords K1 and K2. Firstly, Cartesian tree is a binary search tree about K1, that is, all K1 values of the left subtree of a node are smaller than that of the node, while the right subtree is larger. Secondly, the K2 keywords of all nodes satisfy the order requirement of priority queue (the minimum heap may be used), that is, the K2 value of the node is smaller than that of all nodes in its subtree. Given a binary tree, please judge whether it is a Cartesian tree.
###Input format:
Input first gives a positive integer n ($$$Le $$1000), which is the number of nodes in the tree. Then n lines, each line gives a node information, including: K1 value, K2 value, left child node number, right child node number. Let nodes be numbered sequentially from 0 ~ (n-1). If there is no child node in a node, then the location is given $$- 1 $$.
###Output format:
Output 'yes' if the tree is a Cartesian tree; Otherwise, output ` no '.
###Input sample 1:
```in
six
8 27 5 1
9 40 -1 -1
10 20 0 3
12 21 -1 4
15 22 -1 -1
5 35 -1 -1
```
###Output sample 1:
```out
YES
```
###Input sample 2:
```
six
8 27 5 1
9 40 -1 -1
10 20 0 3
12 11 -1 4
15 22 -1 -1
50 35 -1 -1
```
###Output sample 2:
```
NO
```






answer:If there is no answer, please comment