Data Structures19 sections · 729 units
Open in Course

Validate BST Solution

Implement your solution

Implement BST validation using either the range approach or inorder approach.

Common mistakes:

  • Using \leq 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 O(n)O(n) time, O(h)O(h) space.