Validate a binary search tree. Compare inorder list, recursive bounds and iterative inorder.
Problem
Check that for every node, all values in the left subtree are smaller and all in the right subtree are larger (strictly).
Pattern
Pass constraints down the recursion (min/max bounds), or use the fact that an inorder traversal of a valid BST is strictly increasing.
final class BNode {
int val;
BNode left, right;
BNode(int val, BNode left, BNode right) { this.val = val; this.left = left; this.right = right; }
BNode(int val) { this(val, null, null); }
}Common wrong answer
Checking only left.val < node.val < right.val at each node passes 5(left 4, right 6(left 3, right 7)), which is invalid because 3 sits in the right subtree of 5.
Approach 1: Collect the inorder list and check it is increasing (O(n) time, O(n) space)
import java.util.ArrayList;
import java.util.List;
class ValidateInorderList {
static boolean isValid(BNode root) {
List<Integer> values = new ArrayList<>();
collect(root, values);
for (int i = 1; i < values.size(); i++) if (values.get(i) <= values.get(i - 1)) return false;
return true;
}
private static void collect(BNode n, List<Integer> out) {
if (n == null) return;
collect(n.left, out); out.add(n.val); collect(n.right, out);
}
}Approach 2: Recursive min/max bounds (O(n) time, O(h) space)
Use long bounds (or nullable Integer) so Integer.MIN_VALUE and MAX_VALUE node values work.
class ValidateBounds {
static boolean isValid(BNode root) { return check(root, Long.MIN_VALUE, Long.MAX_VALUE); }
private static boolean check(BNode n, long low, long high) {
if (n == null) return true;
if (n.val <= low || n.val >= high) return false;
return check(n.left, low, n.val) && check(n.right, n.val, high);
}
}Approach 3: Iterative inorder with a previous value (O(n), O(h), stops early) — optimal in practice
import java.util.ArrayDeque;
import java.util.Deque;
class ValidateIterative {
static boolean isValid(BNode root) {
Deque<BNode> stack = new ArrayDeque<>();
BNode cur = root;
Long prev = null;
while (cur != null || !stack.isEmpty()) {
while (cur != null) { stack.push(cur); cur = cur.left; }
cur = stack.pop();
if (prev != null && cur.val <= prev) return false; // first violation ends the search
prev = (long) cur.val;
cur = cur.right;
}
return true;
}
}
class ValidateDemo {
public static void main(String[] args) {
BNode valid = new BNode(5, new BNode(3, new BNode(1), new BNode(4)), new BNode(8, new BNode(6), new BNode(9)));
BNode tricky = new BNode(5, new BNode(4), new BNode(6, new BNode(3), new BNode(7)));
BNode extreme = new BNode(Integer.MAX_VALUE);
for (BNode t : new BNode[] {valid, tricky, extreme}) {
System.out.println(ValidateInorderList.isValid(t) + " " + ValidateBounds.isValid(t) + " " + ValidateIterative.isValid(t));
} // true x3, false x3, true x3
}
}Feasible → optimal
All O(n) time. The inorder list costs O(n) memory; bounds recursion and iterative inorder use O(h) and can stop at the first violation. The iterative version also avoids deep recursion.
Follow-up questions interviewers ask
Try answering each one before you open it.
Related (same pattern): two nodes of a BST were swapped by mistake; recover it.
During inorder, find the first pair where prev > cur (the first misplaced node is prev) and the last such pair (the second misplaced node is cur); swap their values. O(n) time, O(h) space (O(1) with Morris).
Related (same pattern): convert a sorted array to a height-balanced BST.
Pick the middle element as the root and recurse on each half (the "bounds" are now index ranges). O(n). The inverse problem, BST to sorted list, is the inorder traversal.