1.题目描述
Two elements of a binary search tree (BST) are swapped by mistake.
Recover the tree without changing its structure.
二叉搜索树中的两个节点被错误地交换。
请在不改变其结构的情况下,恢复这棵树。
Example 1:
1 | Input: [1,3,null,null,2] |
Example 2:
1 | Input: [3,1,4,null,null,2] |
Follow up:
- A solution using O(n) space is pretty straight forward.
- Could you devise a constant space solution?
进阶:
- 使用 O(n) 空间复杂度的解法很容易实现。
- 你能想出一个只使用常数空间的解决方案吗?
2.Solutions
No Fancy Algorithm, just Simple and Powerful In-Order Traversal
This question appeared difficult to me but it is really just a simple in-order traversal! I got really frustrated when other people are showing off Morris Traversal which is totally not necessary here.
Let’s start by writing the in order traversal:
1 | private void traverse (TreeNode root) { |
So when we need to print the node values in order, we insert System.out.println(root.val) in the place of “Do some business”.
What is the business we are doing here?
We need to find the first and second elements that are not in order right?
How do we find these two elements? For example, we have the following tree that is printed as in order traversal:
6, 3, 4, 5, 2
We compare each node with its next one and we can find out that 6 is the first element to swap because 6 > 3 and 2 is the second element to swap because 2 < 5.
Really, what we are comparing is the current node and its previous node in the “in order traversal”.
Let us define three variables, firstElement, secondElement, and prevElement. Now we just need to build the “do some business” logic as finding the two elements. See the code below:
1 | public class Solution { |
And we are done, it is just that easy!