π One-stop destination for all your technical interview Preparation π
You are given the root of a binary search tree (BST), where the values of exactly two nodes of the tree were swapped by mistake. Recover the tree without changing its structure.
class Solution {
public:
TreeNode *firstNode, *secondNode, *prevNode;
void recoverTree(TreeNode* root)
{
inorder(root);
// swap the values of misplaced nodes
swap(firstNode->val, secondNode->val);
}
void inorder(TreeNode* root)
{
if (root == NULL) return;
inorder(root->left);
if (prevNode!=NULL && root->val < prevNode->val) {
// set first node
if (firstNode == NULL) firstNode = prevNode;
// set/update second node
secondNode = root;
}
// update previous node
prevNode = root;
inorder(root->right);
}
};