Recover Binary Search Tree (Swap Nodes)
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
Two values in a binary search tree have traded places by accident. Locate the pair and exchange them again so the tree is ordered correctly, without adding, removing or moving any node.
Example 1
- Input
- [5,3,null,null,4,2]
- Output
- [5,2,null,null,4,3]
- Why
- Nodes 3 and 4 are swapped. Swapping them back restores the BST property.
Example 2
- Input
- [6,2,8,null,null,5,9]
- Output
- [5,2,8,null,null,6,9]
- Why
- Nodes 6 and 5 are swapped. Swapping them back restores the BST property.
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 BST visits nodes in strictly increasing order. Swapping two nodes breaks this sorted property, creating either one drop (if swapped nodes were adjacent) or two drops (if separated). The first misplaced value is the larger element at the first drop, and the second is the smaller element at the last drop.
Approaches, worst first
Inorder array sorting
time O(n log n) · space O(n)
Record all nodes in an array during inorder traversal. Sort their values, find the two indices where array values differ from the sorted values, and swap the node contents. Effective and straightforward, but allocates linear auxiliary memory.
Explicit stack inorder walk
time O(n) · space O(h)
Iterate through nodes inorder with an explicit stack, tracking the previously visited node. Upon finding `prev.val > curr.val`, assign the first suspect to `prev` (if not already set) and the second to `curr`. Swap values once traversal finishes.
Morris traversal with drop detectionWrite this one
time O(n) · space O(1)
Use threaded binary trees to execute the inorder pass in constant auxiliary space. Detect the inversion pairs while maintaining tree links, restore threaded pointers, and finally swap the two target values.
Where people lose marks · 2
- Adjacent inversion bug: when the two swapped nodes are adjacent in inorder order, only one drop occurs. Failing to assign the second node during the first drop leaves it null.
- Comparing with node value instead of full node pointer: swapping values requires references to the actual tree nodes, not just integer copies.
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 .