Loading...
Loading...
Loading...
A tree is given in level order. Print YES if it is a valid binary search tree and NO if it is not. Every key in a left subtree must be smaller than the node and every key in a right subtree larger. Equal keys make it invalid.
Example 1
Input
2 1 3
Output
YES
Example 2
Input
5 1 6 # # 3 7
Output
NO
3 is in the right subtree of 5 but is smaller than 5.
Try the problem first. Open one hint at a time.
Checking only the parent and its children is not enough.
Pass a low and a high bound down: going left the key becomes the high bound, going right it becomes the low bound.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor