cconnected.
Understand the pattern. Find your own way.Find your starting point
Your learning feed/Idea 48 of 218
Ideas this builds on
Trees48 / 218

A BST inherits promises from every ancestor

Picture

In a BST, being a left child means more than being smaller than your immediate parent. The entire left subtree must obey the ancestor’s upper bound. Validation becomes simple when every node receives the range it is allowed to inhabit.

Predict

Root 10 has left child 5, and that child’s right child is 12. Each immediate left/right comparison looks sensible. Is the whole tree a BST?

Reveal the reasoning

No. The 12 lies inside root 10’s left subtree and must be less than 10. Local parent checks miss a constraint inherited from an earlier ancestor.

Go deeper: carry an interval as context

For unique integer keys, let valid(node,low,high) mean every key in the subtree must satisfy strict bounds. A left child inherits the lower bound and receives the current key as its upper bound; a right child receives the opposite update.

def valid_bst(node, low=float("-inf"), high=float("inf")):
    if node is None:
        return True
    value, left, right = node
    if not low < value < high:
        return False
    return valid_bst(left, low, value) and valid_bst(right, value, high)

invalid = (10, (5, None, (12, None, None)), None)
assert not valid_bst(invalid)
assert valid_bst((10, (5, None, None), (12, None, None)))
assert not valid_bst((10, (10, None, None), None))
assert valid_bst(None)

Time is O(n) in the worst case and stack space O(h). Bounds are a compact summary of all relevant ancestor ordering constraints. An alternative checks that the entire inorder sequence is strictly increasing, carrying only the previously visited key rather than collecting a list.

Search uses the same invariant in the other direction: comparing a target with the current key rules out one whole subtree. Insertion locates the missing search position and attaches there. Both take O(h), so sorted insertion into an unbalanced BST can create a chain and make repeated operations quadratic overall.

Recognition signal: is validity constrained by the full ancestral path? Passing bounds often makes hidden global restrictions local at the current node.

Analogy limit: supporting duplicate keys requires an explicit policy. Allowing equality on both sides indiscriminately changes the strict BST contract and can invalidate other algorithms that assumed unique keys.

Change one rule

The structure is only a min-heap. Can these lower/upper bounds validate it?

Check the variation

No. A heap has parent-to-child ordering and a shape requirement, not a BST’s left-smaller/right-larger partition. Validate the actual invariant of the requested structure.

Connect

Common ancestor can exploit BST ordering when it is present. Invariants explain why inherited promises justify eliminating possibilities.