πDay 13. Fixing Two nodes of a BST π§
The problem can be found at the following link: Question Link
π‘ Problem Description:
Given the root of a Binary Search Tree (BST), where exactly two nodes were swapped by mistake, your task is to fix (or correct) the BST by swapping them back. The structure of the tree should not change.
π Example Walkthrough:
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output:
1
Explanation:
The nodes 20 and 8 were swapped by mistake. After swapping them back, the BST is restored correctly.
Example 2:
Input:
5
/ \
10 20
/ \
2 8
Output:
1
Explanation:
The nodes 10 and 5 were swapped by mistake. After swapping them back, the BST is restored correctly.
Constraints:
$(1 \leq \text{Number of Nodes} \leq 10^3)$
π― My Approach:
Optimized Inorder Traversal (O(N)
Time, O(H)
Space)
O(N)
Time, O(H)
Space)Use an inorder traversal to detect swapped nodes in the BST.
Identify the two misplaced nodes:
If a node appears larger than the next node, it's incorrectly placed.
Track the first misplaced node and the second misplaced node.
Swap the values of the two misplaced nodes to restore the BST.
Algorithm Steps:
Perform an inorder traversal to find the two misplaced nodes.
If the first misplaced node is found, store it in
first
.If a second misplaced node is found later, store it in
last
.If there's no second misplaced node, use the
middle
node instead.Swap the values of the two misplaced nodes.
π Time and Auxiliary Space Complexity
Expected Time Complexity:
O(N)
, since we traverse each node once.Expected Auxiliary Space Complexity:
O(H)
, due to the recursion stack in the inorder traversal.
π Solution Code
Code (C++)
class Solution {
public:
void correctBST(Node* root) {
Node *first = nullptr, *middle = nullptr, *last = nullptr, *prev = nullptr;
function<void(Node*)> inorder = [&](Node* node) {
if (!node) return;
inorder(node->left);
if (prev && node->data < prev->data) {
if (!first) first = prev, middle = node;
else last = node;
}
prev = node;
inorder(node->right);
};
inorder(root);
swap(first->data, last ? last->data : middle->data);
}
};
Code (Java)
class Solution {
Node first, middle, last, prev;
void inorder(Node root) {
if (root == null) return;
inorder(root.left);
if (prev != null && root.data < prev.data) {
if (first == null) {
first = prev;
middle = root;
} else {
last = root;
}
}
prev = root;
inorder(root.right);
}
void correctBST(Node root) {
first = middle = last = prev = null;
inorder(root);
int temp = first.data;
first.data = (last != null) ? last.data : middle.data;
if (last != null) last.data = temp;
else middle.data = temp;
}
}
Code (Python)
class Solution:
def correctBST(self, root):
self.first = self.middle = self.last = self.prev = None
def inorder(node):
if not node:
return
inorder(node.left)
if self.prev and node.data < self.prev.data:
if not self.first:
self.first, self.middle = self.prev, node
else:
self.last = node
self.prev = node
inorder(node.right)
inorder(root)
self.first.data, (self.last or self.middle).data = (self.last or self.middle).data, self.first.data
π― Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated