```
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
return !root ? false : (!root->left && !root->right && root->val == sum) || (root->left && hasPathSum(root->left, sum - root->val)) || (root->right && hasPathSum(root->right, sum - root->val));
}
};
```