Recover Binary Search Tree
A Binary Search Tree (BST) has a property where all nodes in the left subtree of a node are smaller than the node's value, and all nodes in the right subtree are larger. This challenge involves a BST where exactly two nodes have been swapped by mistake. Your task is to identify these two swapped nodes and correct the BST by swapping their values back to their original positions. This problem is crucial for understanding BST properties and for scenarios where data integrity in ordered structures might be compromised.
Problem Description
You are given the root of a binary tree. It is guaranteed that this binary tree was originally a valid Binary Search Tree (BST), but exactly two nodes in the tree were swapped. Your task is to identify these two swapped nodes and restore the BST to its original valid state by swapping their values.
Key Requirements:
- Modify the tree in-place. You should not create a new tree.
- The values of the two swapped nodes should be exchanged.
- The resulting tree must be a valid BST.
Expected Behavior: After your modifications, traversing the tree in-order should produce a sorted sequence of node values.
Edge Cases:
- The swapped nodes might be adjacent in the in-order traversal.
- The swapped nodes might not be adjacent in the in-order traversal.
- The tree could be very small (e.g., only two nodes).
Examples
Example 1:
Input:
1
/ \
3 2
/
0
Output:
3
/ \
1 2
/
0
Explanation:
The in-order traversal of the original tree is [0, 3, 1, 2].
The swapped nodes are 3 and 1.
After swapping their values, the in-order traversal becomes [0, 1, 3, 2], which is not sorted. Oh, wait.
The in-order traversal of the original tree is [0, 1, 3, 2]. This is not sorted, indicating a swap has occurred.
Let's re-evaluate the example input.
Assume the original valid BST had an in-order traversal of [0, 1, 2, 3].
If the nodes with values 3 and 1 were swapped, the tree might look like:
1
/ \
3 2
/
0
In-order traversal: [0, 3, 1, 2].
Here, 3 is greater than its in-order successor 1. This is the first violation.
The second violation is when the in-order successor is smaller than the current node, which is not the case here for 3.
Let's try another example interpretation for clarity.
Consider a BST with the following structure and values:
3
/ \
1 4
\
2
The in-order traversal is [1, 2, 3, 4]. This is a valid BST.
Now, let's swap nodes with values 1 and 3:
1
/ \
3 4
\
2
The in-order traversal of this modified tree is [3, 2, 1, 4].
Here, we have two violations:
1. 3 > 2 (incorrect order between 3 and 2)
2. 2 > 1 (incorrect order between 2 and 1)
The swapped nodes in this case are 3 and 1. After swapping their values back:
3
/ \
1 4
\
2
The in-order traversal is [1, 2, 3, 4], which is sorted.
Therefore, for the input tree representing [3, 2, 1, 4] in-order:
Input Tree Representation (example, not the only way to represent):
3
/ \
2 4
/
1
(Assuming 2 is left child of 3, 1 is left child of 2, 4 is right child of 3)
Output Tree Representation (after swapping values of 3 and 1):
1
/ \
2 4
/
3
In-order traversal: [1, 2, 3, 4]
Example 2:
Input Tree (values in in-order traversal): [5, 2, 8, 3, 9]
This corresponds to a BST where nodes 2 and 8 were swapped.
Original valid BST in-order: [2, 3, 5, 8, 9]
Swapped BST in-order: [5, 2, 3, 8, 9] <- Let's correct this example to be more illustrative of non-adjacent swaps.
Let's use a clear visual.
Original Valid BST:
5
/ \
2 8
/ \
7 9
In-order traversal: [2, 5, 7, 8, 9]
Now, let's swap nodes with values 2 and 8:
5
/ \
8 2
/ \
7 9
In-order traversal: [8, 5, 7, 2, 9]
Here, we see two violations:
1. 8 > 5 (first violation, node 8 is the first incorrect node)
2. 7 > 2 (second violation, node 2 is the second incorrect node)
The nodes to be swapped are 8 and 2.
Input Tree Representation:
5
/ \
8 2
/ \
7 9
Output Tree Representation (after swapping values of 8 and 2):
5
/ \
2 8
/ \
7 9
In-order traversal: [2, 5, 7, 8, 9]
Example 3:
Input Tree (values in in-order traversal): [1, 2]
This corresponds to a BST where nodes 1 and 2 were swapped.
Original valid BST in-order: [1, 2]
Swapped BST in-order: [2, 1]
Input Tree Representation:
2
/
1
Output Tree Representation (after swapping values of 2 and 1):
1
/
2
In-order traversal: [1, 2]
Constraints
- The number of nodes in the tree will be between 1 and 1000, inclusive.
- The values of the nodes will be unique integers within the range of [-1000, 1000], inclusive.
- The input will be the root of a binary tree.
- The solution should aim for an efficient time complexity, ideally O(N), where N is the number of nodes in the tree.
- The solution should aim for an efficient space complexity, ideally O(1) if possible, or O(H) where H is the height of the tree if recursion is used for traversal.
Notes
- A standard in-order traversal of a valid BST visits nodes in ascending order.
- When two nodes are swapped in a BST, there will be at most two places where the in-order traversal sequence is violated.
- Consider how you can identify the nodes that are out of order during an in-order traversal.
- Think about the properties of the first and second nodes that violate the sorted order. They might be adjacent or separated in the in-order sequence.
- The problem states exactly two nodes are swapped. This implies there will always be exactly two nodes whose values need to be exchanged.