```
class Solution {
public:
bool hasPath(TreeNode *node, int rest){
if (node == NULL)// node doesn't exist or this path is wrong
{
return false;
}
else if(node -> left == NULL && node -> right == NULL ) //reached leaf
{
if (rest == 0) //path sum up correctly
return true;
else
return false;
}
else if ( rest <= 0) //hasn't reached leaf but sum decreased to 0 or below already
{
return false;
}
else
{
return hasPath(node->left,rest - node->left->val) || hasPath(node->right,rest - node->right->val);
}
}
bool hasPathSum(TreeNode *root, int sum) {
if(root == NULL)
{
return false;
}
else
{
bool flag = hasPath(root, sum - root->val);
return flag;
}
}
```

};