Recursive: valid(node, lo, hi) — base null return True; if node.val ≤ lo or ≥ hi return False; return valid(left, lo, node.val) and valid(right, node.val, hi).
✎ Whiteboard
3
⌨ Type It
Practice until you don't need to look. Use the guide comments below as scaffolding. The green highlights are the nuances to burn into memory.
# ─── VALIDATE BST (LeetCode 98) ───
# Pattern: Inorder traversal — values must be strictly increasing
# Time: O(n) Space: O(h)
#
# 1. Define: isValidBST(root)
#
# 2. Init stack and prev:
# stack, prev = [], None
#
# 3. Iterative inorder: while stack or root:
#
# 4. Go left, push: while root: stack.append(root); root = root.left
#
# 5. Pop and check: root = stack.pop()
# if prev is not None and root.val <= prev: return False