Given a binary search tree and an integer K, i would like to find the largest element less than K.
In the below tree,
for K = 13, result = 12
for K =
I think the idea here is to record the last node after which you move to the right subtree. Therefore, the code will be (has been updated)
int findNum (Node *node, int K)
{
Node* last_right_move = NULL;
while (node)
{
if (K<=node->data)
node = node->left;
else
{
last_right_move = node;
node = node->right;
}
}
if (last_right_move)
return last_right_move->data;
else
return NOT_FOUND; // defined previously. (-1 may conflict with negative number)
}