/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool hasPathSum(TreeNode *root,int sum,int cursum)
{
if(root==NULL) return false;
else if(root->left==NULL&&root->right==NULL)
{
cursum+=root->val;
return cursum==sum;
}
else
return hasPathSum(root->left,sum,cursum+root->val)||hasPathSum(root->right,sum,cursum+root->val);
}
bool hasPathSum(TreeNode *root, int sum)
{
return hasPathSum(root,sum,0);
}
};
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool hasPathSum(TreeNode *root,int sum,int cursum)
{
if(root==NULL) return false;
else if(root->left==NULL&&root->right==NULL)
{
cursum+=root->val;
return cursum==sum;
}
else
return hasPathSum(root->left,sum,cursum+root->val)||hasPathSum(root->right,sum,cursum+root->val);
}
bool hasPathSum(TreeNode *root, int sum)
{
return hasPathSum(root,sum,0);
}
};