Web Toolbar by Wibiya

Pages

Friday, January 6, 2012

Write a function to check whether there exists a root to leaf path sum equal to a given number.

Solution #1: Here is the recursive code,

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

    sum = sum - root->data;

    return hasSum(root->left, sum) || hasSum(root->right, sum);
}

No comments: