Validate Binary Search Tree
Is the tree a valid BST? Every node must exceed ALL left descendants and be below ALL right ones.
Open on LeetCode ↗Intuition
Checking only parent vs children misses violations deep in a subtree. Carry (min, max) bounds down instead: entering a left child caps the max at the parent; a right child raises the min. Each node checks itself against bounds inherited from every ancestor at once.
Approach
The classic wrong answer
node.left.val < node.val < node.right.val locally is NOT enough — a grandchild can violate a grandparent (e.g. [5,4,6,null,null,3,7], the 3 under 6).
Bounds propagation
valid(node, lo, hi): require lo < node.val < hi; recurse left with hi=node.val, right with lo=node.val.
Alternative: inorder
A BST's inorder is strictly increasing; traverse and compare consecutive values — same O(n).
Solution & live demo
Edge cases
Strict inequalities reject equality — duplicates invalidate.
±∞ initial bounds avoid sentinel-value collisions.