Skip to content

Instantly share code, notes, and snippets.

@rohith2506
Created December 26, 2014 06:53
Show Gist options
  • Save rohith2506/aaab5553ad8e44252060 to your computer and use it in GitHub Desktop.
Save rohith2506/aaab5553ad8e44252060 to your computer and use it in GitHub Desktop.
Recover Binary tree without in constant space when two elements are accidentally swapped
/*
Using morris algorithm
Just traverse the tree and whenerv u find that there are two elements that values neeeds are not in order,
just store them as first and second and swap them.
*/
TreeNode *first = NULL;
TreeNode *second = NULL;
TreeNode *previous = NULL;
void recoverTree(TreeNode *root) {
if (!root) return;
previous = new TreeNode(INT_MIN);
morris_inorder(root);
int t = first->val;
first->val = second->val;
second->val = t;
}
void morris_inorder(TreeNode *root) {
TreeNode *cur = root, *pre = NULL;
while (cur) {
if (cur->left == NULL) {
if (cur->val <= previous->val && !first) first = previous;
if (cur->val <= previous->val && first) second = cur;
previous = cur;
cur = cur->right;
}
else {
// find the predecessor
pre = cur->left;
while (pre->right && pre->right != cur)
pre = pre->right;
if (pre->right == NULL) {
// set the backtrace link
pre->right = cur;
cur = cur->left;
}
else {
if (cur->val <= previous->val && !first) first = previous;
if (cur->val <= previous->val && first) second = cur;
previous = cur;
pre->right = NULL;
cur = cur->right;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment