Implement BST validation using either the range approach or inorder approach.
Common mistakes:
- Using instead of (BSTs typically require strict inequality)
- Only checking immediate children, not entire subtrees
- Not handling integer overflow when using min/max bounds
The inorder approach:
def isValidBST(root):
prev = float('-inf')
def inorder(node):
nonlocal prev
if not node:
return True
if not inorder(node.left):
return False
if node.val <= prev:
return False
prev = node.val
return inorder(node.right)
return inorder(root)
Both approaches are time, space.