Hourly DSABlog

Validate a Binary Search Tree: Carry the Allowed Range

By Hourly DSA · · 4 min read

To validate a binary search tree, check every node against the range allowed by all its ancestors. Comparing a node only with its immediate children misses values that fit locally but violate a rule higher in the tree.

We'll use the strict definition: every value in a node's left subtree is smaller than that node, and every value in its right subtree is larger. Duplicate values are not allowed.

Find the failure in a local check

Suppose the root is 10. Its left child is 5, and its right child is 15. The node 15 has a left child of 6 and a right child of 20.

Every immediate parent-child comparison looks correct. Five is below 10, fifteen is above 10, six is below 15, and twenty is above 15.

But 6 belongs to the root's right subtree. Every value there must be greater than 10. The tree is invalid even though 6 is on the correct side of its own parent.

A validation function must preserve the root's constraint while descending through 15.

Pass down an open interval

Let valid(node, lower, upper) mean that the subtree is a valid BST and every value in it lies strictly between the supplied bounds. A missing bound means there is no restriction on that side.

valid(node, lower, upper):
    if node is null:
        return true
    if lower exists and node.value <= lower:
        return false
    if upper exists and node.value >= upper:
        return false
    return valid(node.left, lower, node.value)
       and valid(node.right, node.value, upper)

Start at the root with both bounds absent. Going left introduces an upper bound. Going right introduces a lower bound. The other bound remains in force.

Use an explicit representation for an absent bound. Choosing the smallest and largest ordinary integers as strict initial bounds would incorrectly reject those values as valid node values.

Follow the invalid value's range

At the root 10, there is no inherited restriction. Its right child must lie above 10, so 15 receives lower bound 10.

Moving left from 15 adds upper bound 15 while preserving lower bound 10. The node 6 must satisfy 10 < 6 < 15. It fails immediately.

That is the missing information in the local approach: 6 must satisfy both ancestors, not just its parent.

Why can two bounds represent every ancestor?

Each left turn imposes an upper limit, and each right turn imposes a lower limit. Their intersection is still an interval.

Once the current node passes its bounds check, using its value as the new bound tightens the allowed interval for that child. No earlier restriction is lost. Repeating this process checks the entire subtree against all the choices that led to it.

This differs from computing tree height, where information returns upward from children. Here, constraints also travel downward from parents.

Decide what duplicates mean

Under our strict definition, a second 10 anywhere in the tree is invalid. Some BST implementations allow equal values on a specified side or store a count inside each node.

Those are different contracts. If duplicates go on the right, for example, the lower bound may be inclusive while the upper bound remains exclusive. Preserve that distinction throughout the recursion.

Don't change both comparisons to allow equality without checking which duplicate policy the problem requires.

Explain the cost

The worst case visits all n nodes, taking O(n) time and O(h) recursion space for height h. An invalid value may let the search stop earlier. A long chain can still require O(n) stack space.

The input is assumed to be a proper binary tree. These checks validate ordering, not whether arbitrary references form an acyclic tree.

Try a value that almost fits

In the original example, replace 6 with 12. Is the tree now valid?

Yes. Twelve lies between 10 and 15, and the other nodes satisfy their inherited ranges.

Practice carrying the full condition through a small example with Hourly DSA. One ancestor constraint can be the difference between a plausible check and a correct one.

See another place where strict and inclusive boundaries determine correctness.

Binary Search Boundaries: Find the First Match Safely →