Two elements of a binary search tree (BST) are swapped by mistake.
Recover the tree without changing its structure.
A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?
public void recoverTree(TreeNode root) {
if (root == null) {
return;
}
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
TreeNode previous = null;
TreeNode p1 = null;
TreeNode p2 = null;
while (cur != null || !stack.isEmpty()) {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
cur = stack.pop();
if (previous != null) {
if (previous.val > cur.val) {
if (p1 == null) {
p1 = previous;
}
p2 = cur;
}
}
previous = cur;
cur = cur.right;
}
int tmp = p1.val;
p1.val = p2.val;
p2.val = tmp;
}