Skip to content

Instantly share code, notes, and snippets.

@vinitshahdeo
Created August 1, 2018 10:19
Show Gist options
  • Save vinitshahdeo/589beeed65940da177b3c63ebea314e5 to your computer and use it in GitHub Desktop.
Save vinitshahdeo/589beeed65940da177b3c63ebea314e5 to your computer and use it in GitHub Desktop.
Inorder Successor - BST
Node * inOrderSuccessor(Node *root, Node *x)
{
if(root==NULL)
return root;
if(x->right)
{
Node* temp=x->right;
while(temp->left!=NULL)
temp=temp->left;
return temp;
}
else
{
Node* successor=NULL;
Node* ancestor=root;
while(ancestor!=x)
{
if(x->data<ancestor->data)
{
successor=ancestor;
ancestor=ancestor->left;
}
else
{
ancestor=ancestor->right;
}
}
return successor;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment