Even the sample program gives the wrong output in this case.

[1, 2147483647]

-2147483648

Use long instead of int to to solve the problem recursively;

```
public boolean hasPathSum(TreeNode root, int sum) {
return safeSearch(root, (long)sum);
}
public boolean safeSearch(TreeNode root, long sum) {
if(root == null) return false;
if(root.left == null && root.right == null) return sum == root.val;
return safeSearch(root.left, sum - root.val) || safeSearch(root.right, sum - root.val);
}
```