```
bool hasPathSumPre(TreeNode* root, int sum){
if (root == NULL) return sum == 0;
return hasPathSumPre(root->left, sum - root->val) || hasPathSumPre(root->right, sum - root->val);
}
bool hasPathSum(TreeNode* root, int sum) {
if (root == NULL) return false;
return hasPathSumPre(root, sum);
}
```