Recover BST


  • 0
    H

    We can solve this in O(n) time and with a single traversal of the given BST. Since inorder traversal of BST is always a sorted array, the problem can be reduced to a problem where two elements of a sorted array are swapped. There are two cases that we need to handle:

    1. The swapped nodes are not adjacent in the inorder traversal of the BST.

    For example, Nodes 5 and 25 are swapped in {3 5 7 8 10 15 20 25}.
    The inorder traversal of the given tree is 3 25 7 8 10 15 20 5
    If we observe carefully, during inorder traversal, we find node 7 is smaller than the previous visited node 25. Here save the context of node 25 (previous node). Again, we find that node 5 is smaller than the previous node 20. This time, we save the context of node 5 ( current node ). Finally swap the two node’s values.

    1. The swapped nodes are adjacent in the inorder traversal of BST.

    For example, Nodes 7 and 8 are swapped in {3 5 7 8 10 15 20 25}.
    The inorder traversal of the given tree is 3 5 8 7 10 15 20 25
    Unlike case #1, here only one point exists where a node value is smaller than previous node value. e.g. node 7 is smaller than node 8.

    How to Solve? We will maintain three pointers, first, middle and last. When we find the first point where current node value is smaller than previous node value, we update the first with the previous node & middle with the current node. When we find the second point where current node value is smaller than previous node value, we update the last with the current node. In case #2, we will never find the second point. So, last pointer will not be updated. After processing, if the last node value is null, then two swapped nodes of BST are adjacent.

    Here is the code for the above

    /* A binary tree node has data, pointer to left child
       and a pointer to right child */
    struct node
    {
        int data;
        struct node *left, *right;
    };
     
    // A utility function to swap two integers
    void swap( int* a, int* b )
    {
        int t = *a;
        *a = *b;
        *b = t;
    }
     
    /* Helper function that allocates a new node with the
       given data and NULL left and right pointers. */
    struct node* newNode(int data)
    {
        struct node* node = (struct node *)malloc(sizeof(struct node));
        node->data = data;
        node->left = NULL;
        node->right = NULL;
        return(node);
    }
     
    // This function does inorder traversal to find out the two swapped nodes.
    // It sets three pointers, first, middle and last.  If the swapped nodes are
    // adjacent to each other, then first and middle contain the resultant nodes
    // Else, first and last contain the resultant nodes
    void correctBSTUtil( struct node* root, struct node** first,
                         struct node** middle, struct node** last,
                         struct node** prev )
    {
        if( root )
        {
            // Recur for the left subtree
            correctBSTUtil( root->left, first, middle, last, prev );
     
            // If this node is smaller than the previous node, it's violating
            // the BST rule.
            if (*prev && root->data < (*prev)->data)
            {
                // If this is first violation, mark these two nodes as
                // 'first' and 'middle'
                if ( !*first )
                {
                    *first = *prev;
                    *middle = root;
                }
     
                // If this is second violation, mark this node as last
                else
                    *last = root;
            }
     
            // Mark this node as previous
            *prev = root;
     
            // Recur for the right subtree
            correctBSTUtil( root->right, first, middle, last, prev );
        }
    }
     
    // A function to fix a given BST where two nodes are swapped.  This
    // function uses correctBSTUtil() to find out two nodes and swaps the
    // nodes to fix the BST
    void correctBST( struct node* root )
    {
        // Initialize pointers needed for correctBSTUtil()
        struct node *first, *middle, *last, *prev;
        first = middle = last = prev = NULL;
     
        // Set the poiters to find out two nodes
        correctBSTUtil( root, &first, &middle, &last, &prev );
     
        // Fix (or correct) the tree
        if( first && last )
            swap( &(first->data), &(last->data) );
        else if( first && middle ) // Adjacent nodes swapped
            swap( &(first->data), &(middle->data) );
     
        // else nodes have not been swapped, passed tree is really BST.
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.