Binary search trees
Start with no coding-interview background, learn to trace an algorithm by hand, measure its cost, and finish able to reason about arrays, hashing, trees, graphs, dynamic programming and the trade-offs an interviewer expects you to name.
What you will be able to do
#- State the binary search tree ordering invariant.
- Search and insert in O(h) where h is the height.
- Validate a binary search tree using value bounds, not just parent comparisons.
Overview
#The ordering invariant
In a binary search tree every node in the left subtree is smaller than the node, and every node in the right subtree is larger. That invariant makes search follow a single path from the root, so search is O(h) where h is the tree height.
function search(root, target) {
let node = root;
while (node) {
if (node.value === target) return node;
node = target < node.value ? node.left : node.right;
}
return null;
}
Height matters
A balanced tree has height about log n, so search is O(log n). A degenerate tree that looks like a linked list has height n, so search is O(n). The invariant alone does not guarantee balance; self-balancing trees such as AVL and red-black trees maintain it.
Inorder gives sorted order
Because left is smaller and right is larger, an inorder traversal visits values in ascending order. This property is the basis of many BST problems, including finding the kth smallest element.
Validating a BST correctly
Comparing each node only with its parent is wrong. A node deep in the left subtree must still be smaller than every ancestor that bounds it. Pass down the allowed range:
function isValid(root, low = -Infinity, high = Infinity) {
if (!root) return true;
if (root.value <= low || root.value >= high) return false;
return isValid(root.left, low, root.value) && isValid(root.right, root.value, high);
}
Each node narrows the range for its children, which enforces the full invariant.
Practice next
- Find the kth smallest value using an inorder traversal.
- Find the lowest common ancestor of two nodes in a BST using the ordering.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
In a BST containing 5, 3, 8, 1, 4, 7, 9, what sequence does inorder traversal produce?
starter.javascript// inorder visits left, node, rightReveal the reference answer
reference.javascriptInorder visits the values in ascending order: [1, 3, 4, 5, 7, 8, 9].Authored expected output: [1, 3, 4, 5, 7, 8, 9]
Sorted order is a defining property of inorder traversal on a BST.
Exercise 2Debug
This validate function wrongly accepts the tree 5 with left child 3 whose right child is 6. Find the flaw.
starter.javascriptfunction isValid(node) { if (!node) return true; if (node.left && node.left.value >= node.value) return false; if (node.right && node.right.value <= node.value) return false; return isValid(node.left) && isValid(node.right); }Reveal the reference answer
reference.javascriptfunction isValid(node, low = -Infinity, high = Infinity) { if (!node) return true; if (node.value <= low || node.value >= high) return false; return isValid(node.left, low, node.value) && isValid(node.right, node.value, high); }Authored expected output: false for the tree with 6 under the left subtree of 5
The parent-only check misses that 6 is greater than its grandparent 5, violating the BST invariant.
Exercise 3Explain
Explain why a degenerate BST makes search O(n).
Reveal the reference answer
reference.textIf each node has only one child, the tree is a chain of height n. Search follows one node per level, so it visits up to n nodes, which is the same as linear search. Balance keeps the height near log n and preserves the speed advantage.Authored expected output: A chain of height n means up to n steps, identical to linear search.
Height, not node count alone, determines BST search cost.
Common misconceptions
#- A binary search tree is always balanced. It can degenerate to a line unless a balancing scheme is used.
- Checking each node against its parent validates a BST. Bounds from all ancestors are required.
- Search in a BST is always O(log n). It is O(h), which is O(n) for a degenerate tree.
Follow-up practice
#- dsa-from-zero-heaps-and-priority-queues
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.