Monday, October 6, 2014

Recover Binary Search Tree

Problem

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?

Idea

把BST树转入数组,然后从前往后扫数组,找到第一个顺序不对的数,然后从后往前扫,找到第二个顺序不对的。交换两个的val就ok了.

Solution


No comments:

Post a Comment