Write a function that returns the number of common entries in two BTs.
Difficulty level
This exercise is mostly suitable for students
Btree belong_Btree_rec(Btree B, int v)
{
if (B)
{
if (B->data == v)
return B;
return (belong_Btree_rec(B->left, v) || belong_Btree_rec(B->right, v))?B:NULL;
}
return NULL;
}
int nb_common_entries(Btree B, Btree C)
{
int nbl, nbr;
if(B)
{
nbl = nb_common_entries(C, B->left);
nbr = nb_common_entries(C, B->right);
if(belong_Btree_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 !!
Implementation of a stack to hold elements of 2 different types