mediumTreeDFSBinary Search TreeTrees

Validate Binary Search Tree

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

Signals to notice

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

Brute force first

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.

The key insight

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.

What must stay true

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.

Easy way to go wrong

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