Recover Binary Search Tree - Problem
Recover Binary Search Tree is a fascinating tree problem that tests your understanding of BST properties and traversal techniques.

You're given the root of a binary search tree (BST) where exactly two nodes have been swapped by mistake. Your task is to identify and fix these two nodes without changing the tree's structure - only swap their values back to restore the BST property.

๐ŸŽฏ Goal: Detect the two incorrectly placed nodes and swap their values
๐Ÿ“ฅ Input: Root of a BST with exactly 2 nodes swapped
๐Ÿ“ค Output: Fix the tree in-place (no return value needed)

The challenge lies in efficiently identifying which two nodes are out of place using the BST's inherent ordering property.

Input & Output

example_1.py โ€” Basic Swap Case
$ Input: root = [1,3,null,null,2]
โ€บ Output: root = [3,1,null,null,2]
๐Ÿ’ก Note: The tree has nodes 1 and 3 swapped. In a valid BST, we should have 3 at root with 1 and 2 as children. After swapping values of nodes with values 1 and 3, the tree becomes a valid BST.
example_2.py โ€” Adjacent Nodes Swapped
$ Input: root = [3,1,4,null,null,2]
โ€บ Output: root = [2,1,4,null,null,3]
๐Ÿ’ก Note: Two adjacent nodes in inorder traversal are swapped. Original inorder: [1,3,2,4]. After swapping nodes 3 and 2, inorder becomes [1,2,3,4] which is correctly sorted.
example_3.py โ€” Non-Adjacent Nodes
$ Input: root = [2,3,1]
โ€บ Output: root = [2,1,3]
๐Ÿ’ก Note: Nodes 1 and 3 are swapped. In the corrected BST, left child should be smaller than root, and right child should be larger than root.

Visualization

Tap to expand
๐Ÿ•ต๏ธ BST Recovery Detective WorkThe Case: Two Books Misplaced in Library๐Ÿ“š Library Shelf (BST): Books should be arranged by ID number๐ŸŽฏ Problem: Exactly 2 books are in wrong positionsEvidence Collection Methods:๐Ÿ” Brute Force Detectiveโ€ข Check every book pairโ€ข Very thorough but slowโ€ข O(nยณ) time complexity๐Ÿ’ก Like checking all possiblecombinations manually๐Ÿ“ Smart Detectiveโ€ข Record all book positionsโ€ข Compare with sorted listโ€ข O(n log n) time๐Ÿ’ก Like making a list andchecking against ideal orderโšก Expert Detectiveโ€ข Walk through onceโ€ข Spot violations instantlyโ€ข O(n) time, O(1) space๐Ÿ’ก Like trained eye spottingout-of-place items instantly๐Ÿ” The Crime Pattern:โ€ข In sorted sequence [1,2,3,4], swapping creates violationsโ€ข Swap 2โ†”4 gives [1,4,3,2]: violations at 4>3 and 3>2โ€ข First violation gives us first suspect, second gives us second suspect๐ŸŽฏ Key Insight:Two swapped elements in sorted arraycreate at most 2 violation points:โ€ข Where larger comes before smallerโ€ข Morris traversal finds these in O(1) space!
Understanding the Visualization
1
Understand the Crime Scene
A valid BST has the property that inorder traversal gives sorted sequence. Two swapped nodes disrupt this order.
2
Look for Evidence
In the inorder sequence, violations occur where prev > current. This gives us clues about swapped nodes.
3
Identify Suspects
First violation: previous node is likely first suspect. Second violation: current node is likely second suspect.
4
Make the Arrest
Swap the values of the two identified nodes to restore the BST property.
Key Takeaway
๐ŸŽฏ Key Insight: Morris traversal allows us to detect BST violations during inorder traversal using only O(1) extra space, making it the optimal solution for space-constrained environments.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through all nodes using Morris traversal

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only constant extra space for pointers, no recursion stack

n
2n
โœ“ Linear Space

Constraints

  • The number of nodes in the tree is in the range [2, 1000]
  • Node values are in the range [-231, 231 - 1]
  • Exactly two nodes are swapped - no more, no less
  • The tree structure remains unchanged, only values are swapped
Asked in
Microsoft 45 Amazon 38 Google 32 Meta 28
89.0K Views
Medium-High Frequency
~25 min Avg. Time
2.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen