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?
解法1: In-order Traversal O(N)
BST的很重要的性质就是对他进行in order traversal的话,得到的是一个有序数组。
那么可以按照in order traversal,找到第一个node使得node.val >= node.successor.val和最后一个node使得node.predecessor.val >= node.val就可以了
|
|