Problem Summary
Two elements of a binary search tree are swapped. Recover the tree by swapping them again.
Solution 1
We can get the inorder traversal of the tree, and then the two nodes needed are easy to find. The time and space complexities are both O(N).
Solution 2
Suppose the two swapped nodes are i and j, their values being vi and vj (vi > vj). Then there are two possible situations:
i and j are adjacent in the inorder traversal of the tree.
Otherwise, there would be two nodes x and y, whose values being vx and vy, such that x is the successor of i in the inorder traversal, and y is the predecessor of j.
To find i and j, we use inorder traversal.
We can use two pointers p and q to remember the pointers to i and j, and a pointer pre to remember the predecessor of the current node. During the traversal, if we find that pre->val > cur->val, then we check p. If p has not been assigned with any value, we let p = pre and q = cur; else, we only let q = cur.
After the search, we switch the two nodes that p and q point to.
The time complexity of this method is also O(N), but the space it requires is only O(1).
Code for Solution 2
|
|