Validate Binary Search Tree

MediumTrees
Next
TreeBSTDFS

A valid binary search tree has, for every node, all values in its left subtree strictly smaller and all values in its right subtree strictly larger. Decide whether the given tree is a valid BST. Build the tree from the level-order input yourself, then solve the problem.

Input

A single line giving the tree in level order, with the word null for a missing child (for example 3 9 20 null null 15 7).

Output

Print true if the tree is a valid BST, otherwise false.

Constraints

1 <= number of nodes <= 2000 -10^9 <= value <= 10^9

Example 1
Input
2 1 3
Output
true

1 < 2 < 3.

Example 2
Input
5 1 4 null null 3 6
Output
false

The right child 4 is smaller than 5, so it is invalid.

Hints

Validate Binary Search Tree - Medium Trees Problem | Skill Shiksha