Article start
DSA Course: Interview Patterns and Problem Solving
Module 6: Trees

Validate Binary Search Tree: Range bounds Pattern

Validate every node against ancestor-defined lower and upper bounds.

May 29, 2026·25

Learning Outcome

After this lesson, you should be able to explain why checking only parent-child relationships is not enough for validating a BST.

Problem Statement

Given the root of a binary tree, return true if it is a valid binary search tree. Every node in the left subtree must be smaller than the current node, and every node in the right subtree must be larger.

InputOutputWhy
[2,1,3]trueAll nodes satisfy BST bounds.
[5,1,4,null,null,3,6]false3 is in the right subtree of 5 but is less than 5.

Brute Force Approach

Check every node only against its direct children.

This catches local violations but misses deeper ancestor-bound violations, so it is logically incomplete.

Optimized Approach

Carry a valid range into each recursive call. A node must be strictly greater than the lower bound and strictly less than the upper bound. Left children tighten the upper bound; right children tighten the lower bound.

Exact Pseudocode

valid(node, low, high):
  if node is null:
    return true
  if node.val <= low or node.val >= high:
    return false
  return valid(node.left, low, node.val)
     and valid(node.right, node.val, high)
return valid(root, -infinity, infinity)

Reference Code

class Solution:
    def isValidBST(self, root):
        def valid(node, low, high):
            if not node:
                return True
            if node.val <= low or node.val >= high:
                return False
            return valid(node.left, low, node.val) and valid(node.right, node.val, high)

        return valid(root, float("-inf"), float("inf"))

Sample Dry Run

NodeAllowed rangeResult
5(-inf, inf)Valid, split bounds
1(-inf, 5)Valid
4(5, inf)Valid locally, check children
3(5, 4)Invalid because 3 is not greater than 5

Complexity

MeasureValueReason
TimeO(n)Each node is checked once.
SpaceO(h)The recursion stack depends on tree height.

Edge Cases

  • Duplicate values should fail in the strict BST version.
  • Very small or very large integer values need wide bounds.
  • Ancestor violations deeper in the tree.

Interview Checklist

  • Do not check only direct children.
  • Carry lower and upper bounds through recursion.
  • Use strict inequalities for the common BST definition.

FAQs

Why do parent-child checks fail?

A node can satisfy its parent but still violate an older ancestor's bound.

Why use long bounds in Java/C++?

Node values may equal integer extremes, so wider bounds avoid false failures.

What is the core pattern?

DFS with inherited range bounds.

Test your knowledge

Take a quick quiz based on this chapter.

Discussion

0 comments

Sign in to share a question or add to the discussion.
Start the discussion

Ask a question or share what stood out to you.