Simple. I think, no explanation is required for this.
Get summation values of right sub-tree and left sub-tree, and add them to current node's data and update.
Get summation values of right sub-tree and left sub-tree, and add them to current node's data and update.
/*Define the tree, such that the parent node always contains the sum of children nodes.*/
int sumUp(struct node *root){
if(root==NULL){
return 0;
}else{
int l=sumUp(root->left);
int r=sumUp(root->right);
root->data=root->data + l + r;
return root->data;
}
}
No comments:
Post a Comment