[LeetCode] 99. Recover Binary Search Tree

Problem

Two elements of a binary search tree (BST) are swapped by mistake.

Recover the tree without changing its structure.

Example 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Input: [1,3,null,null,2]

   1
  /
 3
  \
   2

Output: [3,1,null,null,2]

   3
  /
 1
  \
   2

Example 2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Input: [3,1,4,null,null,2]

  3
 / \
1   4
   /
  2

Output: [2,1,4,null,null,3]

  2
 / \
1   4
   /
  3

Follow up:

  • A solution using O(n) space is pretty straight forward.
  • Could you devise a constant space solution?

Explanation 1

  1. If we use O(n) space, we can use in-order traversal method to solve this problem. In a valid BST, if we use in-order traversal, all traversaled elements should be sorted, if it is not sorted, then this is not a valid BST.

  2. If the correct valid BST is [1, 2, 3, 4, 5, 6, 7, 8], if two elements are swaped mistake, let say 2 and 6 are swapped so we want to create two new variable to record 2 and 6, let say first and second, and now the traversal elements become [1, 6, 3, 4, 5, 2, 7, 8]. We find out 6 and 3 are not sorted in order (first is 6, second is 3), also 5 and 2 are not sorted in order (first remains 6, second is 2). So we swap first and second and we are done.

Solution 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    TreeNode pre = null;
    TreeNode first = null;
    TreeNode second = null;

    void helper(TreeNode root) {
        if (root == null) return;
        helper(root.left);
        if (pre != null && pre.val >= root.val) {
            if (first == null) first = pre;
            second = root;
        }
        pre = root;
        helper(root.right);
    }

    public void recoverTree(TreeNode root) {
        helper(root);
        if (first != null && second != null) {
            int temp = first.val;
            first.val = second.val;
            second.val = temp;
        }
    }
}

Explanation 2

  1. If we want O(1) space, then we can apply morris traversal method.

Solution 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public void recoverTree(TreeNode root) {
        TreeNode pre = null;
        TreeNode first = null;
        TreeNode second = null;
        while (root != null) {
            if (root.left != null) {
                TreeNode temp = root.left;
                while (temp.right != null && temp.right != root) {
                    temp = temp.right;
                }
                if (temp.right == null) {
                    temp.right = root;
                    root = root.left;
                } else {
                    temp.right = null;
                    //visit root.val
                    if (pre != null && pre.val >= root.val) {
                        if (first == null) first = pre;
                        second = root;
                    }
                    pre = root;
                    root = root.right;
                }
            } else {
                //visit root.val
                if (pre != null && pre.val >= root.val) {
                    if (first == null) first = pre;
                        second = root;
                    }
                pre = root;
                root = root.right;
            }
        }
        if (first != null && second != null) {
            int temp = first.val;
            first.val = second.val;
            second.val = temp;
        }
    }
}