LeetCode 99 Recover Binary Search Tree (C++, Python)

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?



Solution C++


 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 binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
    TreeNode* prev;
    TreeNode* wrong1;
    TreeNode* wrong2;
    void visitNode(TreeNode* node) {
        if (prev && prev->val > node->val) {
            if (wrong1) {
                wrong2 = node;
            } else {
                wrong1 = prev;
                wrong2 = node;
            }
        }
        prev = node;
    }
public:
    void recoverTree(TreeNode *root) {
        prev = wrong1 = wrong2 = NULL;
        TreeNode* node = root;
        while (node) {
            if (node->left == NULL) {
                visitNode(node);
                node = node->right;
            } else {
                TreeNode* pre = node->left;
                while (pre->right != NULL && pre->right != node) {
                    pre = pre->right;
                }
                if (pre->right == NULL) {
                    pre->right = node;
                    node = node->left;
                } else {
                    pre->right = NULL;
                    visitNode(node);
                    node = node->right;
                }
            }  
        }
        swap(wrong1->val, wrong2->val);
    }
};

Solution Python


 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
# Definition for a  binary tree node
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def inOrder(self, root):
        if root is None: return
        for n in self.inOrder(root.left):
            yield n
        yield root
        for n in self.inOrder(root.right):
            yield n
    # @param root, a tree node
    # @return nothing, modify the binary tree in-place instead.
    def recoverTree(self, root):
        prev = None
        wrong1 = None
        wrong2 = None
        for n in self.inOrder(root):
            if prev:
                if prev.val > n.val:
                    if wrong1 is None:
                        wrong1 = prev
                        wrong2 = n
                    else:
                        wrong2 = n
                        break
            prev = n
        wrong1.val, wrong2.val = wrong2.val, wrong1.val
        

Popular posts from this blog

How Django Works (4) URL Resolution

Python Class Method and Class Only Method

How Django works (3): Testing Server and Static Files Serving