Does the Tree Really Obey the Search Rule
Implement isValidBst
You are given the root of a binary tree of integers. Decide whether it is a valid binary search tree: for every node, all values in its left subtree must be strictly smaller and all values in its right subtree strictly larger than the node's own value. Note that this concerns every descendant, not just the two children. Repeated values make the tree invalid. An empty tree is valid.
Listing the inorder values and checking that they strictly increase works. A single pass that hands each node the range of values it is allowed to have is more direct and stops at the first violation.
Example 1:
Input: root = [8,4,12,2,6,10,14]
Output: true
Example 2:
Input: root = [10,5,15,null,null,6,20]
Output: false
Example 3:
Input: root = [5,5]
Output: false
+ 14 hidden test cases run on Submit.
Constraints:
- ●
0 ≤ number of nodes ≤ 100; −1000 ≤ node.val ≤ 1000; values may repeat - ●
The tree is given as its root node (null for an empty tree); each node has a val, a left child and a right child - ●
The tree is a valid binary search tree when, for EVERY node, all values in its left subtree are STRICTLY smaller and all values in its right subtree are STRICTLY larger (not just its two children — every descendant). A repeated value therefore makes the tree invalid - ●
Return true if the tree is a valid binary search tree, false otherwise; an empty tree is valid
root =