Consider a binary tree of integers dynamically implemented; write a function update that changes node values such that each node is equal to the sum of its children. It is obvious that leaves remain unchanged.
Example:
Difficulty level
This exercise is mostly suitable for students
void update(Btree *B)
{
if(*B==NULL || (*B)->left==NULL && (*B)->right==NULL) return ;
update(&((*B)->left));
update(&((*B)->right));
if((*B)->left && (*B)->right)
(*B)->data = (*B)->left->data + (*B)->right->data;
else
if((*B)->left)
(*B)->data = (*B)->left->data ;
else
if((*B)->right)
(*B)->data = (*B)->right->data;
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Maximum distance between points