Friday, 10 February 2012

[Amazon Online Test]: Finding the in-order successor

Given a binary search tree with the following node structure.
struct tree
{ int data;
  struct tree *left, *right , *next;
};
The next pointer of each pointer is pointing to NULL. Write a code to initialize the next pointer of each node to its in-order successor.

Here is my code, I have not tested it. Hope it works. Time complexity is O(n).

int save=NULL;
void Setsuccessor(struct tree *root)
{
           if(root==NULL)
              return;

          Setsuccessor(root->left);
         
           if(save!=NULL)
              save->next=root;
           save=root;

         Setsuccessor(root->right);
}
         

No comments:

Post a Comment