Web Toolbar by Wibiya

Pages

Saturday, January 14, 2012

Given a binary tree, store the sum of left and right subtrees in a node.

Solution #1: Here is the recursive code,

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

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

    int temp = root->data;

    root->data = lsum + rsum;

    return root->data + temp;
}

No comments: