Loading...
Loading...
Loading...
A price tree is valid when, for every node, all values in its left side are strictly smaller and all values in its right side are strictly larger. Print YES if the tree is valid and NO if not. An empty tree is valid.
Example 1
Input
5 3 8 2 4 7 9
Output
YES
Valid.
Example 2
Input
5 3 8 2 6 7 9
Output
NO
The 6 is on the left of 5 but larger than 5.
Try the problem first. Open one hint at a time.
Comparing a node only with its parent is not enough.
Pass a lower and an upper limit down to each child.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor