This problem follows the Recursive Range Check pattern, commonly found in the Trees category. Recognizing this pattern is key to solving it efficiently in an interview setting.
Pass min/max bounds. Each node must be within bounds. Recurse with updated bounds.
Checking just parent-child isn't enough — a node must satisfy ALL ancestors. Passing bounds down ensures this globally.
def isValidBST(node, lo=-inf, hi=inf):
if not node: return true
if node.val <= lo or node.val >= hi:
return false
return isValidBST(node.left, lo, node.val) and isValidBST(node.right, node.val, hi)Practice Validate Binary Search Tree and similar Trees problems with flashcards. Build pattern recognition through active recall.
Practice this problem