A binary search tree (BST) follows these rules at every node:
- All values in the left subtree are smaller than the value of the node.
- All values in the right subtree are larger than the value of the node.
- The left subtree and the right subtree are also BSTs.
Given a binary tree, return true if it is a valid BST, and false if it is not. Equal values are not allowed.
How the judge passes the data
Examples
Example 1:
Input: root = [8,4,12,2,6,10,14]
Output: true
Explanation: Every node is bigger than everything on its left and smaller than everything on its right.
Example 2:
Input: root = [20,10,30,null,null,15,35]
Output: false
Explanation: 15 sits in the right subtree of 20, but 15 is smaller than 20. This breaks the rule.
Example 3:
Input: root = [7,7]
Output: false
Explanation: The left child is equal to its parent, not smaller. This is not a valid BST.