mediumTreeDFSBinary Search TreeTrees

Validate Binary Search Tree

mediumTime: O(n)Space: O(h)

Recognize the pattern

check if binary search tree is validnode values within rangein-order gives sorted

Brute force idea

A straightforward first read of Validate Binary Search Tree is this: Check each node only against its parent — misses violations deeper in the tree. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.

Better approach

The real unlock in Validate Binary Search Tree comes when you notice this: Pass valid range (min, max) down: each node must be within its allowed range. Instead of recomputing the world every time, you preserve just enough context to let the next decision become obvious.

Key invariant

The compass for Validate Binary Search Tree is this: Every node has an allowed range; left children tighten the max, right children tighten the min. As long as that statement keeps holding, you can trust the steps built on top of it.

Watch out for

A common way to get lost in Validate Binary Search Tree is this: Only checking immediate parent-child — a node can violate the BST property with a grandparent. Most mistakes here are not about syntax; they come from losing track of what your state, pointer, or structure is supposed to mean.

Trees Pattern