intermediate8 min read·Updated September 29, 2026

Validate a BST Explained: Tracing [5,4,6,null,null,3,7]

Master BST validation with a clear mental model. Walk through the [5,4,6,null,null,3,7] example to see why local checks fail and how to use bounds.

By Learnisim AI·Published September 29, 2026
LEARNING ARCSetup → Model in the example → Full trace → Twist → Edge cases → Apply
PREREQUISITES
  • Binary tree traversal (DFS/recursion)
  • Basic binary search tree properties
Validating BST [5, 4, 6, null, null, 3, 7] — The Bounded Window Model Binary Tree Structure & Windows 5 (-inf, inf) 4 (-inf, 5) 6 (5, inf) 3 FAIL (5, 6) 7 (6, inf) Local Check Trap: 3 < 6 (looks valid locally) Global Violation: 3 is in right subtree of 5 (not > 5) Recursive Bounded Validation Steps 1. Root Check (5) Window: (-inf, inf) ✓ Valid. Branches left & right. 2. Left Subtree (4) Window: (-inf, 5). 4 < 5 is True. Leaf node returns true. 3. Right Subtree & Left Child (3) — VIOLATION Node 6 updates lower bound to 5: range (5, inf). Node 3 updates upper bound to 6: range (5, 6). 3 is out of range! Result: NOT A VALID BST (returns false) Global interval constraints successfully catch hidden flaws.
Is [5,4,6,null,null,3,7] a BST? overview diagram
Why

Why local node checks fail when trying to validate a binary search tree

Phase 1: The Trap of Local Comparisons

Imagine you are handed the binary tree representation [5, 4, 6, null, null, 3, 7]. Your goal is to determine whether it is a valid Binary Search Tree (BST) where every left descendant is strictly less than its ancestor and every right descendant is strictly greater. If you only check local relationships—asking whether each child is correctly positioned relative to its immediate parent—you will easily miss global violations hidden deeper in the branches.
Consider the root node 5. Its left child is 4 () and its right child is 6 (). Moving down to the right child 6, its left child is 3 and its right child is 7. Locally, 3 is less than 6, and 7 is greater than 6. Everything looks correct at first glance. However, a deeper inspection reveals a critical flaw: 3 sits inside the right subtree of 5, yet , violating the foundational rule of a BST.
Without a global mechanism to track allowed value boundaries across the entire path from the root, local checks give a false sense of security. We need a systematic way to propagate constraints down the tree so that distant ancestors can restrict the valid ranges of their descendants.
Why Local BST Validation Fails (Tree: [5, 4, 6, null, null, 3, 7]) Local parent-child checks miss deep global boundary violations Tree Topology & Local Checks < 5 (ok) > 5 (ok) < 6 (Local OK) > 6 (ok) 5 4 6 3 7 The Trap: 3 < 6 (Passes Local Check) But 3 is in the RIGHT subtree of 5! Rule broken: All right descendants must be > 5. Global Range Propagation (Correct) Root(5) Allowed Range: (-∞ Valid because 5 is within (-∞, +∞) Right Child(6) Range: (5, +∞) Lower bound updated to 5 from ancestor Left Child of 6 (Node 3) Range: (5, 6) Value is 3. Is 5 < 3 < 6 ? FALSE! Global check instantly flags BST violation! Takeaway: Pass [min, max] down trees Local checks ignore inherited constraints.
Why local node checks fail when trying to validate a binary search tree diagram
Model

The Bounded Window Mental Model for BST Validation

Phase 2: The Bounded Window Model

To see why a global rule is required for the tree [5, 4, 6, null, null, 3, 7], we must abandon the local view where a node only checks its immediate children. Instead, imagine each node in the tree as living inside a strictly enforced valid numerical window, defined by an open interval
.
When we start at the root node 5, it has no ancestors, meaning its allowed value range is initially unbounded: . As we branch down into the tree, we narrow this window based on the path we take:
- Moving to the left child (4) means all values in that entire left subtree must be strictly less than 5. Our upper bound updates to 5, yielding the window .
- Moving to the right child (6) means all values in that entire right subtree must be strictly greater than 5. Our lower bound updates to 5, yielding the window .
Now, trace what happens when we descend from root 6 to its left child, which holds the value 3. Because 3 is in the left child position of 6, our upper bound becomes 6, creating the active window for that node. But wait! The node's value is 3, and 3 does not fall inside . This geometric violation instantly flags the tree as invalid without ever needing to look further down.
5 (-inf, inf)
/ \
4 6 (5, inf)
/ \
3 7
[fails (5, 6)]
Try this: Given root = 5, left = 4, right = 6. What is the active validation window for the right child 6's right child 7?
BST Validation: The Bounded Window Mental Model Why local child checks fail on tree: [5, 4, 6, null, null, 3, 7] 5 ( -∞, +∞ ) 4 ( -∞, 5 ) 6 ( 5, +∞ ) 3 Window (5, 6) 7 ( 6, +∞ ) Violation Detected! Node 3 lives in the left subtree of 6, inheriting window (5, 6). Since 3 is not within (5, 6), the tree is INVALID instantly. Why Local Checks Fail Locally, 3 < 6 looks correct. But globally, 3 violates its ancestor 5's lower bound requirement (> 5).
The Bounded Window Mental Model for BST Validation diagram
Worked example

Tracing the Bounded Window Through [5,4,6,null,null,3,7]

Phase 3: Walking the Tree with Strict Bounds

Now that we model BST validation as passing down a valid numeric interval , let us run our recursive function on our locked example: the tree represented by [5, 4, 6, null, null, 3, 7]. We will trace every step, every interval update, and every return value.

Given

- Root node: 5
- Left child of 5: 4 (leaf node)
- Right child of 5: 6
- Left child of 6: 3
- Right child of 6: 7

Steps

1. Root Check: We start at the root node 5 with an initial valid window of . Is 5 strictly between and ? Yes. We branch to both children, narrowing our window:
- Left branch receives:
- Right branch receives:
2. Left Subtree (4): We evaluate node 4 with the window . Is ? Yes. Node 4 has no children, so its recursive calls return true. The left branch of the tree is valid.
3. Right Subtree (6): We evaluate node 6 with the window . Is ? Yes. Now, we narrow the window further for 6's children:
- Left child of 6 receives: because upper bound drops to node 6's value.
- Right child of 6 receives: because lower bound rises to node 6's value.
4. Deep Left Child (3): We evaluate node 3 with the window . We check the condition . This evaluates to false because 3 is not greater than 5.

Result

The recursive call for node 3 immediately returns false. This failure bubbles up through node 6 and back to the root, confirming the entire tree is invalid.
python
# Intermediate state during validation of node 3:
# validate(node=3, low=5, high=6)
# check: 5 < 3 -> False
python
def isValidBST(root, low=float('-inf'), high=float('inf')):
    if not root:
        return True
    if not (low < root.val < high):
        return False
    return (isValidBST(root.left, low, root.val) and 
            isValidBST(root.right, root.val, high))
Tracing Bounded Window: [5, 4, 6, null, null, 3, 7] Step 4 Failure: Node 3 violates window (5, 6) 5 (-∞ , ∞) 4 (-∞, 5) 6 (5, ∞) 3 Window: (5, 6) 7 (6, ∞) validate(node=3, low=5, high=6) Check 5 < 3 < 6 fails! returns False Bounded Window Logic • Left branch: upper bound = parent val • Right branch: lower bound = parent val • Node 3 breaks rule (not > 5)
Tracing the Bounded Window Through [5,4,6,null,null,3,7] diagram
Practice

Practice Validating a Modified BST Subtree Range

Phase 4: Practice Your Validation Trace

Now that you have traced the recursive window validation on the original tree [5,4,6,null,null,3,7], it is time to test your mental model on a modified structure. Consider what happens when we fix that rogue right-subtree node. Suppose our tree is now [5, 4, 6, null, null, 5, 7] where node 3 has been replaced by 5 as the left child of 6.
Take out a notepad and trace the window for this modified tree. Start at the root 5 with bounds . Go left to 4 with bounds , and right to 6 with bounds . When you evaluate the left child of 6, which now holds value 5, what does the validation check low < node.val < high evaluate to? Remember that a strict BST requires every node in the right subtree to be strictly greater than the root 5.
Work through the recursive calls for each node from top to bottom, recording the exact interval at every step before deciding whether the final result is true or false.
Try this: Tree: [5, 4, 6, null, null, 5, 7]
Question: Is this modified tree a valid BST? Trace the window for node 5 (left child of 6).
Apply

Transferring the Bounded Window Pattern to a Mirror BST Variant

Phase 5: Transfer

We have seen how our locked example [5, 4, 6, null, null, 3, 7] exposes a global violation hidden deep in the right subtree. Local parent-child checks (node.left.val < node.val < node.right.val) miss the fact that node 3 is in the right subtree of root 5 and violates the global invariant. By passing down an accumulating bounded window , our recursive traversal correctly flagged 3 as out of bounds.
Now, let us transfer this exact interval-tracking mindset to a structural variant of our locked example. Imagine we mutate the tree layout while keeping the same core values: what if we place a rogue node in the left subtree that violates the upper bound? The transfer challenge is to adapt your mental model from checking lower bounds to simultaneously managing both upper and lower boundaries in unfamiliar tree geometries.
When encountering new binary tree validation problems—such as checking if a tree is a valid Max-Heap or verifying BST properties under duplicate rules—the key is to identify what parameter acts as the accumulating constraint. Just as the window contracts at every step for a BST, other tree properties rely on passing state down the call stack rather than inspecting single nodes in isolation.

FAQ

Why does the example [5,4,6,null,null,3,7] evaluate to false?
Even though node 6's immediate children (3 and 7) satisfy local rules, node 3 lives in the right subtree of root 5. A valid BST requires all nodes in the right subtree to be strictly greater than 5.
Why do local node checks fail when validating a binary search tree?
Checking only if a node's left is smaller and right is larger misses global violations deep down the tree, because a descendant in the wrong subtree can still satisfy local parent-child comparisons.
What is the time complexity of the bounded window BST validation algorithm?
The time complexity is O(N) because every node in the tree is visited at most once during the recursive validation process.
What is the space complexity due to recursion?
The space complexity is O(H), where H is the height of the tree, representing the maximum call stack depth during recursion.

Keep learning