# [LeetCode] Recover Binary Search Tree (Java)

Two elements of a binary search tree (BST) are swapped by mistake.

Recover the tree without changing its structure.

Note:
A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?

## Analysis

We can use in-order traverse to find the swapped element. During the traverse, we can find the element that is smaller than the previous node. Using this method we can find the swapped node. Save it and swap them. Done.

## Complexity

The time complexity is $O(n)$. But the space complexity is not constant, since we use recursive function.

## Follow-up

After searching, I found there is a way to use $O(1)$ space to do the in-order traverse, which is called Morris traverse.

The Morris traverse is like the following.

Firstly, take the root node as current node.

Then there are two possibilities.

1. If current node doesn’t have left child, output the value. And current = current.right.
2. If current node has left child, try to find the precursor node of current node, which is the right-most node of the left child of current. If the right child of it is null (If we don’t modify the tree, it should be null), set current as its right child, and current = current.left. Otherwise (It means that we have modify the tree and we have traverse all nodes in the left subtree of current node), set it to null, output current. And current = current.right.

During the traverse, we can find the nodes which are needed to be swapped.

The space complexity of this algorithm is $O(1)$.

But what about the time complexity? In fact, we only visit every edge twice. One is for going to that node, and another one is for searching the precursor node. There are only $n-1$ edges in a tree. So the time complexity is also $O(n)$.

The Morris traverse can also be used in pre-order and post-order traverse. There is a great article, including the detail pictures. You can visit http://www.cnblogs.com/AnnieKim/archive/2013/06/15/MorrisTraversal.html if you are interested.