Responsive Ads Here

Monday, 15 May 2017

Root to leaf path sum

problem id:--http://practice.geeksforgeeks.org/problems/root-to-leaf-path-sum/1


code:--

bool hasPathSum(Node *root, int sum)
{
static int s=0;
if(root==NULL)
    return false;
s=s+root->data;
if(root->left==NULL && root->right==NULL)
    {
    if(s==sum)
        {
        s=s-(root->data);
        return true;
        }
    s=s-(root->data);
    return false;
    }
bool b1=false,b2=false;
if(root->left!=NULL)
    b1=hasPathSum(root->left,sum);
if(root->right!=NULL)
    b2=hasPathSum(root->right,sum);
s=s-(root->data);
return b1||b2;
//return false;
}

No comments:

Post a Comment