Saturday, August 18, 2012

Check whether there exists a root to leaf path sum equal to a given number


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:

Post a Comment