Solution:
1. Do reverse inorder traversal.
2. Set inorder successor to the previous node.
Source Code:
// Given node structure -
struct Node
{
int data;
Node* left;
Node* right;
Node* inorderSuccessor;
};
void BSTree::fillInorderSuccessor(Node* node, Node*& prev)
{
if(node)
{
fillInorderSuccessor(node->right, prev);
node->inorderSuccessor = prev;
prev = node;
fillInorderSuccessor(node->left, prev);
}
}
1. Do reverse inorder traversal.
2. Set inorder successor to the previous node.
Source Code:
// Given node structure -
struct Node
{
int data;
Node* left;
Node* right;
Node* inorderSuccessor;
};
void BSTree::fillInorderSuccessor(Node* node, Node*& prev)
{
if(node)
{
fillInorderSuccessor(node->right, prev);
node->inorderSuccessor = prev;
prev = node;
fillInorderSuccessor(node->left, prev);
}
}
If you are calling the function, it should be called like this :
ReplyDeletefillinorderSuccessor(root, NULL);
Yes it should be........
Delete