Validate a binary search tree
Mediumtreebinary search treedfs
A tree is a valid BST if, for every node, all values in its left subtree are smaller and all values in its right subtree are larger (no equal values).
Examples write a tree level by level, with null for a missing child: [3,9,20,null,null,15,7].
Example 1
Input: root = [2,1,3] Output: true
Example 2
Input: root = [5,1,4,null,null,3,6] Output: false
The 3 is on the right of 5 although it's smaller.
Constraints
- 1 ≤ number of nodes ≤ 10⁴
- -2³¹ ≤ Node.val ≤ 2³¹ - 1
Submit checks your solution against 10 tests, including hidden ones.
// Provided by the judge: keep these definitions
class TreeNode {
constructor(val = 0, left = null, right = null) {
this.val = val
this.left = left
this.right = right
}
}
/**
* @param {TreeNode|null} root
* @return {bool}
*/
function isValidBST(root) {
}