Write a function that returns the number of common entries in two BSTs.
Difficulty level
This exercise is mostly suitable for students
BST belong_bst_rec(BST B, int v)
{
if (B)
{
if (B->data == v)
return B;
if (B->data > v)
return belong_bst_rec(B->left, v);
else
if (B->data < v)
return belong_bst_rec(B->right, v);
}
return NULL;
}
int nb_common_entries(BST B, BST C)
{
int nbl, nbr;
if(B)
{
nbl = nb_common_entries(C, B->left);
nbr = nb_common_entries(C, B->right);
if(belong_bst_rec(C, B->data))
return 1 + nbl +nbr ;
else
return nbl +nbr;
}
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Static Binary Search Trees