Pattern this teaches:Binary Search Tree Problems
Recover Binary Search Tree
A medium BST interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- BST
- Sheets
- 1
- Core for
- 2 roles
The problem
Someone swapped the values of two nodes in a binary search tree, so the ordering is now broken in two places. Find those two nodes and swap them back, leaving the shape of the tree untouched.
Example 1
- Input
- [5,6,7,1,4,3,8]
- Output
- [5,3,7,1,4,6,8]
- Why
- Nodes 3 and 6 were swapped. Inorder before fix: 1,6,4,5,3,7,8. After swapping back: 1,3,4,5,6,7,8.
Example 2
- Input
- [10,5,15,2,12,7,20]
- Output
- [10,5,15,2,7,12,20]
- Why
- Nodes 7 and 12 were swapped. Inorder before fix: 2,5,12,10,7,15,20. After swapping back: 2,5,7,10,12,15,20.
Example 3
- Input
- [1]
- Output
- [1]
- Why
- A single node is already a valid BST.
Constraints
- The number of nodes is in the range [2, 104].
- -231 <= Node.val <= 231 - 1
How to think about it
Updated 2026-09-29An inorder traversal of a valid BST must produce strictly ascending values. When exactly two nodes are swapped, that sequence exhibits either one inversion (if the swapped nodes were adjacent in the sorted order) or two inversions (if they were separated). Catching those inversion boundaries during the traversal pinpoints both misplaced nodes without altering a single pointer.
Approaches, worst first
Inorder dump and sort
time O(n log n) · space O(n)
Traverse inorder to extract all nodes into a list. Sort the collected values and compare against the original traversal to find the two mismatched nodes, then rewrite their values. Simple, but takes linear auxiliary space.
Inorder traversal tracking drops
time O(n) · space O(h)
Walk inorder while maintaining a `prev` pointer. At the first dip where `prev.val > curr.val`, record `prev` as the first corrupted node and `curr` as the second. If a second dip appears, update the second node to `curr`. Swap their values at the end.
Morris inorder traversalWrite this one
time O(n) · space O(1)
Thread predecessor pointers to traverse inorder in O(1) auxiliary space while applying the same two-drop detection rule. Restores all tree threads on the fly and finishes with a single value swap between the two identified nodes.
Where people lose marks · 2
- Adjacent swap trap. When adjacent elements are swapped, there is only one inversion where `prev.val > curr.val`. Forgetting to initialize the second node on the first inversion leaves the second target unset.
- Mutating structural pointers instead of node values. The problem asks to restore the tree without changing its structure; swapping pointers invites circular references.
The theory behind it
BST — the ground this problem stands on. All BST problems
What BST is
A binary search tree is a binary tree that enforces a strict ordering rule at every node. Every value stored in a node's left branch must be smaller than the node itself, and every value in its right branch must be larger. Because this rule holds true everywhere down the tree, searching for a value does not require checking every branch. At each step, a single comparison lets you discard an entire half of the remaining nodes.
When to reach for it
Reach for a binary search tree when problems mention sorted tree structures, finding the kth smallest element, checking tree validity, or searching within a range of values. Signals include queries for the next greater element in a dynamic set, finding the lowest common ancestor in a sorted tree, or converting sorted arrays into height-balanced trees. When data must remain sorted while supporting continuous insertions and lookups, BST properties are the target.
How the pattern works
Use the ordering property to prune entire subtrees. When looking for a key, move left if the target is smaller than the current node, or move right if it is larger, stopping when values match or when reaching a null link. For validating a tree, do not merely compare a node to its immediate children; pass valid lower and upper value bounds down through recursive calls. Walking a valid binary search tree in-order visits all values in strictly increasing numerical order.
More BST problems
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .