> For the complete documentation index, see [llms.txt](https://timmybeeflin.gitbook.io/cracking-leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://timmybeeflin.gitbook.io/cracking-leetcode/tree/112.-path-sum-1.md).

# 112. Path Sum

![](/files/-Mbiy3Z0SOU1QrE4ZwcV)

![](/files/-Mbiy79RrNs7Zu-qKmrf)

## keyword: root to leaf

time: O(n)

space: O(n)

```java
class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) return false;
        if (root.left == null && root.right == null) {
            return targetSum == root.val;
        }
        return hasPathSum(root.left, targetSum - root.val) || 
        hasPathSum(root.right, targetSum - root.val);
    }
}
```
