Want to write something, because this spent maths me for a few hours, in Sunday, in addition to running on the field for two hours, it is on the problem.
Read blogs:
http://www.lifeincode.net/programming/leetcode-recover-binary-search-tree-java/
Http://www.cnblogs.com/AnnieKim/archive/2013/06/15/MorrisTraversal.html
C # implementations:
Did not understand the algorithm, wrote a C # version, from reading code, see what does not understand the place.
Https://github.com/jianminchen/Leetcode_C-/blob/master/99RecoverBinarySearchTree.cs
Simple recursive version, however, finding two violations is not in place.
Https://github.com/jianminchen/Leetcode_C-/blob/master/99RecoverBinarySearchTreeB.cs
Finally understand, start to write a few small functions, see can remember, easy maintenance, test case is not very clear. more satisfactory code. Look at the notes, and after a few months, hopefully you can recall the algorithm in a few minutes.
Work on extracting small functions, and then, understand the algorithm better.
Https://github.com/jianminchen/Leetcode_C-/blob/master/99RecoveryBinarySearchTree_C.cs
Leetcode 99:recovery Binary Search tree finally understand the algorithm, write the code clearly, so that the error is nowhere to hide.