Recover Binary Search Tree
描述
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?
分析
O(logn)空间的解法是,中序递归遍历,用两个指针存放在遍历过程中碰到的两处逆向的位置。
本题要求O(1)空间,只能用 Morris 中序遍历。