How do you validate a binary search tree?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Validating a binary search tree means checking more than whether each node is larger than its left child and smaller than its right child. A correct validator must prove that every value in the entire left subtree is less than the node and every value in the entire right subtree is greater.
Understand the BST Rule Precisely
For a tree to be a valid binary search tree, each node must satisfy a global ordering constraint:
- All values in the left subtree are smaller than the current node.
- All values in the right subtree are larger than the current node.
- The same rule must hold recursively for every subtree.
That “global” part is where many incorrect solutions fail. Consider this tree:
6 is less than 15, so a local child check would accept it, but it still violates the BST property because it appears in the right subtree of 10 and should therefore be greater than 10.
Validate with Lower and Upper Bounds
The most reliable approach is to pass an allowed value range down the tree.
This works because each recursive call tightens the allowed interval. A node in the right subtree inherits the parent’s lower bound, and a node in the left subtree inherits the parent’s upper bound.
The time complexity is O(n) because each node is visited once. The extra space is O(h) for the recursion stack, where h is the tree height.
In-Order Traversal as an Alternative
A second valid strategy uses the fact that an in-order traversal of a BST produces values in strictly increasing order.
This iterative version avoids recursion and is often useful in languages or environments where deep recursion is undesirable.
The in-order approach is elegant, but the bounds approach is usually easier to explain when teaching the core invariant directly.
Decide How to Handle Duplicates
Some BST definitions allow duplicates on one side only, while others require strict ordering with no duplicates at all. The comparison operators in your validator must match the definition your problem expects.
The examples above use strict comparisons:
- Left side must be strictly less than the parent.
- Right side must be strictly greater than the parent.
If duplicates are allowed on one side, change the comparison logic intentionally rather than by accident.
Why Local Checks Are Not Enough
A tempting but incorrect solution is:
This fails because it compares each node only with its immediate children. BST validity depends on all ancestors, not just the parent.
Common Pitfalls
The most common bug is validating only parent-child relationships instead of carrying full lower and upper bounds through recursion.
Another issue is handling duplicates incorrectly. If the problem expects a strict BST, use <= and >= in the failure conditions, not just < and >.
People also forget that an empty tree is a valid BST. Returning False for None breaks many otherwise correct recursive solutions.
Finally, watch out for hard-coded numeric sentinels such as minimum and maximum integers. Using None for unbounded ranges is often cleaner and avoids accidental overflow assumptions.
Summary
- A valid BST must satisfy ordering rules across entire subtrees, not just direct children.
- The bounds-based recursive solution is the most reliable general approach.
- An in-order traversal works because BST values appear in strictly increasing order.
- Duplicate handling depends on the exact BST definition in the problem.
- Empty trees are valid, and local child checks alone are not sufficient.
Related reading
- How do you write a program to find if certain words are similar?
- How do you write a recursive function using a non-recursive stack?
- How does a ''diff'' algorithm work, e.g. in VCDIFF and DiffMerge?
- How does a Resolution algorithm work for propositional logic?
- How do you visualize a ward tree from sklearn.cluster.ward_tree?
- How does a ArrayList's contains() method evaluate objects?
- How does a sorting network beat generic sorting algorithms?
- How does Amazon's Statistically Improbable Phrases work?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.