Quick revision
The short answer
Checking whether a binary tree is a binary search tree is a binary search tree problem solved by passing an allowed range down the tree. Each node must lie strictly between a low and a high limit set by its ancestors; going left, the node's value becomes the new high, and going right, it becomes the new low. Every node is checked once, so it takes O(n) time and O(h) space for the recursion.
The solution
What it costs
| Approach | Time | Extra space | Correct? |
|---|---|---|---|
| Compare each node with its children | O(n) | O(h) | no, misses limits from above |
| Max of left subtree, min of right, at every node | O(n squared) for a chain | O(h) | yes |
| In-order walk, values must keep rising | O(n) | O(h) | yes |
| Pass the allowed range down | O(n) | O(h) | yes |
What to remember
We checked a binary search tree by carrying an allowed range down from the root: turning left makes the node's value the new high limit, turning right makes it the new low limit, and a node outside its range means false. It takes O(n) time and O(h) space. The clue to remember is the word every in the BST rule, which limits a node by all its ancestors, and what a node owes its ancestors is passed down as parameters.
1What it means for a tree to be a binary search tree
Picture a small town where the roads branch like a family tree, with a numbered house at every junction. At each junction a signpost says the same thing: lower house numbers, turn left; higher house numbers, turn right. A delivery driver looking for house 6 needs no map. She reads each sign, turns, and arrives in a few steps. That promise, that the signs never lie, is what a binary search tree is.
Let us say it in the words of the subject, one term at a time. A binary tree is a set of nodes, each holding a value and having at most two children, a left child and a right child; the node at the top is the root. A node's left subtree is its left child and everything below it. A binary search tree, or BST, is a binary tree in which, for every node, every value in its left subtree is smaller than the node and every value in its right subtree is larger.
Notice the word every in that rule. Every value anywhere below on the left must be smaller, however deep it sits, and most wrong answers come from forgetting this one word.
The question gives us the root of a binary tree and asks whether it is a binary search tree. That is smaller than it sounds. We build nothing and change nothing; we only look at each node and ask whether the signposts allow it to be there. Our example tree has root 8, with 4 on its left and 10 on its right; 4 has children 2 and 6, and 10 has only a right child, 12.
Every value on the left of a node is smaller than it, and every value on its right is larger, at every level.
- nodes
- 6
- expected
- true
1Root 8 has 4 and 10 below it. Everything under 4 (2 and 6) is smaller than 8, and everything under 10 (just 12) is larger. This tree is a binary search tree.
What to assume
- Are duplicates allowed?
- We assume not, so a value equal to an ancestor breaks the rule.
- What are the values?
- Java ints of any size, including
Integer.MAX_VALUE. - Is an empty tree a BST?
- Yes, because no node breaks the rule, and a single node is one too. We return a boolean.
Let us check it by hand before any code. Under 8 on the left we find 4, 2 and 6, all smaller than 8. Under 8 on the right we find 10 and 12, both larger. Under 4 on the left is 2, smaller than 4, and on the right is 6, larger than 4. Under 10 on the right is 12, larger than 10. Every rule holds, so the answer is true.
A careful candidate asks a few questions first. Are duplicates allowed? We assume not, so a value equal to an ancestor breaks the rule. What are the values? Java ints of any size, including Integer.MAX_VALUE, which will matter later. Is an empty tree a BST? Yes, because no node breaks the rule, and a single node is one too. We return a boolean.
Your turn
Solve it before you read on. Above is one example with the result expected for it. Under What to assume are the points this solution takes as given. Say your approach aloud or type it below, and write the code in your own editor. Then take one hint at a time and compare.
2How to spot the pattern in a tree validation question
When a beginner reads this question for the first time, the natural plan is to look at each node and its two children and ask whether the left is smaller and the right is larger. It feels like the whole rule, because those are the only nodes a node touches. We will see that a small tree fools this plan, and seeing why is the first step to the right idea.
So let us read the question slowly and take its clues one at a time. The first clue is the phrase binary search tree itself. It tells us the left smaller, right larger rule is being tested, and that rule is about whole subtrees, so an answer that looks only one level down checks a weaker rule than the one asked.
The second clue is that we must give a yes or no about the whole tree. That means visiting every node, usually with recursion, a method that calls itself on the left child and on the right child. What we must settle is what each call needs to know.
The third clue is hidden in the signpost picture. House 6 is legal only if it respects every junction the driver passed: she turned left at 8, so 6 must be below 8, then right at 4, so 6 must be above 4. A node is constrained by its ancestors, the nodes on the path from the root down to it, so that information has to travel downwards, from parent to child, as arguments to the recursive call.
Together these clues point to the binary search trees family, and to a technique we can call passing an allowed range down the tree. A range is a pair of limits, low and high, and a value is inside it when it is strictly between them. The rule to carry to new problems is this: if a node's answer depends on its ancestors, pass what they require down as parameters, and if it depends on the nodes below it, return what they found back up.
Two patterns look similar but do not fit as they stand. Comparing a node only with its children is too weak, as we will see. A level-order walk with a queue reaches every node but forgets the path to each one, so it knows no limits unless we store them beside each node, which is the range idea again. The in-order walk does fit, and we meet it in the follow-up questions.
3The honest first attempts, and the shortcut that fools us
Let us begin with the plan most people write first, because its failure teaches us the real rule. At every node we check that the left child is smaller and the right child is larger, then do the same for both children. Our program keeps it as parentOnly so that we can watch it go wrong.
Here is the tree that fools it. The root 8 has children 4 and 10, and 4 has children 2 and 9. Every parent and child pair looks fine, yet 9 lives in the left subtree of 8, where every value must be smaller than 8.
The picture below shows parentOnly at work, and its last frame tries to find 9 by following the signposts. The search fails even though 9 is in the tree. The shortcut checked only the junction just above each house and forgot the junctions further up, while node 9 must be larger than 4 and also smaller than 8.
So what is the honest version that really follows the definition? At every node, find the largest value in its left subtree and make sure it is smaller than the node, find the smallest value in its right subtree and make sure it is larger, and repeat for every node below. This is our brute force, and its code comes after the picture. It is slow but correct, and a correct slow answer is a good thing to have in hand.
The tree is 8 with 4 and 10 below it, and 2 and 9 below 4. Each frame shows what parentOnly compares.
- checking
- 8 with 4 and 10
- verdict
- fine
1At the root,
parentOnlycompares 8 with its two children. 4 is smaller and 10 is larger, so the root looks fine.- checking
- 4 with 2 and 9
- verdict
- fine
2At node 4 it compares 4 with 2 and 9. 2 is smaller and 9 is larger, so this node looks fine too.
- parentOnly
- true
- correct answer
- false
3Nodes 2, 9 and 10 have no children to compare, so
parentOnlyreturns true for the whole tree.- rule broken
- 9 is under 8's left
- search for 9
- not found
4Now search for 9 the BST way. At 8 we turn right because 9 is larger, reach 10, and find no left child. The 9 sits on the wrong side of 8.
Let us count its work on our good example tree. At 8, the helpers walk 4, 2 and 6 on the left and 10 and 12 on the right; at 4 they walk 2 and 6; at 10 they walk 12. That is eight helper visits beside the six node checks, fourteen in all, which is what our traced run counts.
That does not sound bad, so let us try the shape that hurts, a chain of nodes 1 to 5, each the right child of the one before. The helpers walk four nodes at node 1, three at node 2, and so on, 15 visits in all, and for a chain of 1,000 nodes our run counts 500,500. That is about n squared divided by two, so the time is O(n squared), read as order n squared: the work grows with the square of the number of nodes. The space is O(h), where h is the height, the number of levels, because the recursion keeps one waiting call per level.
Where exactly is the waste? Node 12 is visited by minOf when we check 8, again when we check 10, and once more when we check 12 itself. Each node is re-read once for every ancestor, and every ancestor asks it the same kind of question: are you within my limits? The next section answers all of those questions in one visit.
4Getting to the range check, one observation at a time
Let us start from the waste we just found. Node 12 was visited three times because three ancestors each wanted to know whether it obeyed their rule. What if the ancestors told the node their rules on the way down, so that it could check itself once? That is the signpost driver again: by the time she reaches a house, she knows every turn she took, and every turn is a limit.
So let us see what a turn means. Turning left at a node with value v means everything from here on must be smaller than v, so v becomes an upper limit. Turning right at v means everything from here on must be larger than v, so v becomes a lower limit. Each call therefore carries two numbers, low and high, and the node's value must lie strictly between them. The root has no ancestors, so it starts with no limits at all.
Now the question a beginner asks here. If we pass only two numbers, do we not forget the older limits? Let us try it on node 6. It must be below 8 and above 4. A limit from an ancestor further up would be looser than these two, because 8 and 4 were themselves checked against it on the way down. So the tightest lower limit and the tightest upper limit say everything, and two numbers are enough.
Here is the rule in full. Going to the left child, keep low and make the current value the new high. Going to the right child, make the current value the new low and keep high. If any node falls outside its range, the answer is false. If we fall off the tree onto a null child, there is nothing to break the rule, so that call returns true.
The table runs this check on the tree that fooled the shortcut. Look at the row for 9, which inherited the upper limit 8 from the root, two levels up.
| Node | Allowed range | Inside? |
|---|---|---|
| 8 | (-inf, +inf) | yes |
| 4 | (-inf, 8) | yes |
| 2 | (-inf, 4) | yes |
| 9 | (4, 8) | no, 9 is not below 8 |
| 10 | never checked | the && stopped the search |
Why is this correct, not just plausible? A tree is a binary search tree exactly when every node is smaller than all the ancestors where we turned left and larger than all the ancestors where we turned right. The range we carry holds the tightest of those limits, because each step only ever tightens one side. So checking each node against its range checks all of its ancestors at once, with constant work per node.
One practical trap remains, the numbers that mean no limit at the root. Integer.MIN_VALUE and Integer.MAX_VALUE are tempting, but then a root holding Integer.MAX_VALUE fails the strict test. So the code uses Long.MIN_VALUE and Long.MAX_VALUE, which lie outside every int, so no real value can collide with them.
5Watching the ranges travel down the example tree
The picture runs the range check on our good tree, 8 with 4 and 10, where 4 has 2 and 6 and 10 has 12. The highlighted node is the one being checked, and the panel beside it shows its low and high limits. Nodes already checked are marked as done.
Watch two things. First, the order: the method finishes the whole left side of a node before it starts the right side, because the left call is written first. Second, the limits: each step down changes exactly one of them, and which one depends on whether we turned left or right.
The highlighted node is being checked. Beside the drawing are the limits it inherited from the nodes above it.
- node
- 8
- low
- -inf
- high
- +inf
- inside?
- yes
1We call check on the root with no limits at all, shown as -inf and +inf. 8 is inside, so we go on to its left child first.
- node
- 4
- low
- -inf
- high
- 8
- inside?
- yes
2Going left from 8 makes 8 the new upper limit. Node 4 must lie in (-inf, 8), and it does, so we go left again.
- node
- 2
- low
- -inf
- high
- 4
- inside?
- yes
3Going left from 4 makes 4 the upper limit. Node 2 must lie in (-inf, 4), and it does. Its children are null, which count as fine.
- node
- 6
- low
- 4
- high
- 8
- inside?
- yes
4Back at 4 we go right, so 4 becomes the lower limit and 8 stays the upper one. Node 6 must lie in (4, 8), and it does.
- node
- 10
- low
- 8
- high
- +inf
- inside?
- yes
5The left side of the root is finished. Going right from 8 makes 8 the lower limit, so node 10 must lie in (8, +inf), and it does.
- node
- 12
- low
- 10
- high
- +inf
- inside?
- yes
610 has no left child. Going right makes 10 the lower limit, so node 12 must lie in (10, +inf), and it does.
- checks made
- 6
- result
- true
7Every node sat inside its range, so all the calls return true and
isBstreports true. Six nodes, six range checks.
Let us read the important frames back. In the second frame we turned left from 8, so 8 became the high limit for 4. In the fourth we came back to 4 and turned right, so 4 became the low limit for 6, while the high limit 8 stayed, inherited from the root. Node 6 is checked against both ancestors at once, where the shortcut would have checked it against 4 alone.
Check the last frame yourself. Six nodes were checked, each exactly once, and no helper re-read anything. On the bad tree the same walk stops at 9, whose range is (4, 8), and the && means 10 is never visited.
6The complete Java program
Here is the full program, with a small Node class, isBst, the method check that carries the range, and the broken parentOnly for comparison. main checks the good tree, the bad tree with both methods, a duplicate, a node holding Integer.MAX_VALUE and an empty tree; each comment shows what that line prints.
Let us read it in the order it runs. isBst is a thin starting point. It calls check on the root with the widest possible range, Long.MIN_VALUE and Long.MAX_VALUE, because the root has no ancestors to limit it.
Inside check, the first line handles a null node. Reaching null means we stepped past a leaf, and an empty subtree cannot break the rule, so we return true; this is also why isBst(null) prints true.
The second line is the real test. A value at or below low, or at or above high, is outside its range, so we return false at once. The comparisons include equality because duplicates are not allowed, which is why the tree with a 5 under another 5 prints false.
The last line makes the two recursive calls: left with low unchanged and node.val as the new high, right with node.val as the new low and high unchanged. The && means the right side is checked only if the left side passed, so we stop at the first broken node.
Notice that node.val is an int while low and high are longs. Java widens the int for each comparison, so a node holding Integer.MAX_VALUE is inside the root's range and the program prints true.
7How much it costs, and the cases at the edges
Let us count on the example first. The range check made six checks for six nodes and no extra visits. For n nodes it makes at most n checks, so the time is O(n), growing in step with the number of nodes. We cannot do better, because one misplaced node anywhere could break the rule, so every node must be looked at.
The extra space is the recursion. Each call waits on its child, so at most h calls wait at once, where h is the height, which gives O(h) space. For a balanced tree h is about log n, roughly 20 for a million nodes; for a chain it is n, and a very long chain can overflow Java's call stack.
| Approach | Time | Extra space | Correct? |
|---|---|---|---|
| Compare each node with its children | O(n) | O(h) | no, misses limits from above |
| Max of left subtree, min of right, at every node | O(n squared) for a chain | O(h) | yes |
| In-order walk, values must keep rising | O(n) | O(h) | yes |
| Pass the allowed range down | O(n) | O(h) | yes |
The edge cases need little code. An empty tree returns true from the null test. A single node is compared with the widest range and passes. Duplicates are rejected by the strict comparisons; if the interviewer's rule is that equal values go to the left, we let a value equal its high limit, testing node.val > high instead, while the low side stays strict. Whatever the rule, agree on it before writing code.
8Follow-up questions
Can you solve it with an in-order traversal instead?
Yes. An in-order traversal visits the left subtree, then the node, then the right subtree, and in a binary search tree that lists the values from smallest to largest, 2, 4, 6, 8, 10, 12 for our good tree. We walk in order, remembering the previous value, and return false the moment a value is not larger than it. On the bad tree the order is 2, 4, 9, 8, and the step from 9 to 8 goes down, so we catch it. It is also O(n) time and O(h) space. Keep the previous value as a Long that starts as null, so the first value has nothing to compare against.
How would you avoid recursion for a very deep tree?
We keep our own stack, an ArrayDeque of records holding a node with its low and high. We push the root with the widest range, then repeatedly pop a record, check the node, and push its children with their tightened ranges. The work is the same, O(n), but the waiting entries live on the heap instead of the call stack, so a long chain no longer overflows.
What is the most common bug in this problem?
Comparing each node only with its children, the shortcut we drew, because those are the only nodes a node touches. The second is starting with Integer.MIN_VALUE and Integer.MAX_VALUE as the limits, which rejects a valid tree containing one of those values; a single node holding Integer.MAX_VALUE is a quick test.
9Conclusion
We checked a binary search tree by carrying an allowed range down from the root: turning left makes the node's value the new high limit, turning right makes it the new low limit, and a node outside its range means false. It takes O(n) time and O(h) space. The clue to remember is the word every in the BST rule, which limits a node by all its ancestors, and what a node owes its ancestors is passed down as parameters.
Open with
Let me first confirm the rules: no duplicates, so comparisons are strict, values are any int, and an empty tree counts as valid. The tempting check compares each node only with its two children, but that misses limits from further up; for example a 9 placed under 4, which is the left child of 8, passes every parent and child check while breaking the rule for 8.
Then, if they want more
Following the definition literally, comparing each node with the maximum of its left subtree and the minimum of its right, is correct but O(n squared) on a chain. Instead I will pass an allowed range down the recursion. The root gets no limits, which I represent with Long.MIN_VALUE and Long.MAX_VALUE so that a node holding Integer.MAX_VALUE is still accepted.
When I go left, the current value becomes the new upper limit, and when I go right, it becomes the new lower limit. If any node is not strictly inside its range, I return false, and a null child returns true.
Each node is checked once, so the time is O(n), and the recursion depth is the height, so the space is O(h). An in-order walk that checks the values keep rising is an equally good alternative.
About this answer
- Last updated
- 14 November 2026
- Written for
- Java 21, using only java.util
- Checked
- The Java solution was compiled and run, and each printed result matches the comment beside it. The pictures are checked against the format when the site is built. The explanation around the code is a study note: confirm anything critical against the documentation below. What the checks cover
- Where the question comes from
- Public interview write-ups, reworded by Scrivio; counted 27 times in that collection. How it is counted
- Official documentation
- java.util, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Check whether a binary tree is a binary search tree.": https://scrivio-interview-library.netlify.app/data-structures/solutions/check-whether-a-binary-tree-is-a-binary-search-tree-1ba170/