class Solution {

public:

bool hasPathSum(TreeNode *root, int sum) {

static int res_sum = 0;

bool res;

```
if (root == NULL)
return false;
res_sum += root->val;
if (root->left == NULL && root->right == NULL)
{
if (res_sum == sum)
return true;
else
{
res_sum -= root->val;
return false;
}
}
res = hasPathSum(root->left, sum);
if (res == false)
res = hasPathSum(root->right, sum);
res_sum -= root->val;
return (res);
}
```

};