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
๐ฏ 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.
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
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
โ Linear Growth
Space Complexity
O(1)
Only constant extra space for pointers, no recursion stack
โ 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code