Given the root of a binary tree, determine if it is a valid binary search tree (BST). A valid BST requires every node in the left subtree to have a value strictly less than the node, every node in the right subtree to have a value strictly greater than the node, and both subtrees must also be valid BSTs.
root = [2,1,3]trueroot = [5,1,4,null,null,3,6]falsevalidate(root, −∞, +∞) — root starts with the full valid rangenode == null → return true (null is trivially valid)node.val ≤ min OR node.val ≥ max → return false (range violation)(min, node.val), right with (node.val, max) — range narrows each levelA common mistake is only checking the immediate parent-child relationship. That misses deeper violations — e.g., a node in the right subtree that is smaller than the root. By threading the valid range (min, max) down each call, every node carries the full historical constraint from all its ancestors. Going left means the current value becomes the new ceiling; going right it becomes the new floor. The check is globally correct in a single O(n) pass.
The common mistake is to compare each node only with its own children. In this tree every parent child pair looks fine: 4 is less than 5, 6 is more than 5, 3 is less than 6, and 7 is more than 6. So the local check says valid, but the tree is not a search tree.
Instead, pass down a range that every node must fit inside. The root may hold any value, so its range is minus infinity to plus infinity. Going left, the node value becomes the new upper bound. Going right, the node value becomes the new lower bound. If any node falls outside its range, the tree is invalid.
Compare against the whole range, not just the parent. An inorder traversal that is strictly increasing is the other classic check.
Run the animation above on your own input, or watch the 2:17 video explanation of Validate Binary Search Tree with a dry run and Java code.