Saturday, August 18, 2012

Store the sum of left and right subtrees in a node


int sum(struct node* root)
{
    if(root == NULL)
        return 0;

    int lsum = sum(root->left);
    int rsum = sum(root->right);

    root->data = lsum + rsum + root->data;

    return root->data;
}

No comments:

Post a Comment