Description
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)的算法,可以用O(1)实现,但是太巧妙了,不太好想,我选择放弃。对于二叉排序树,可以建立其与有序数组的联系,方便处理一些问题,而二叉排序树的中序遍历即为有序数组。对于只变换一次的数组,要达到完全有序,有两种情况:
a. 1,2,4,3,5
b. 1,2,5,4,3
观察发现,出现了相邻的逆序数时,可以先假设就是交换这两个数,如果之后还是出现了相邻的逆序数,则更新待更新的数。
具体而言,采用三个指针,pre用于逆序数的探测,first,second用于记录待交换的数。
具体代码如下:
1 | class Solution { |