# CODE

```python
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
        if not root:
            return False
        
        #####################
        ##### RECURSIVE #####
        #####################
        
        def helper(curr, curr_sum):
            if curr_sum == targetSum and not curr.left and not curr.right:
                return True
            
            left = helper(curr.left, curr_sum + curr.left.val) if curr.left else False
            right = helper(curr.right, curr_sum + curr.right.val) if curr.right else False
            return left or right
        return helper(root, root.val)
    
        #####################
        ##### ITERATIVE #####
        #####################
            
        stack = [(root, root.val)]
        while stack:
            cur, pathsum = stack.pop()
            if not cur.left and not cur.right and pathsum == targetSum:
                return True
            if cur.left:
                stack.append((cur.left, pathsum + cur.left.val))
            if cur.right:
                stack.append((cur.right, pathsum + cur.right.val))
        return False
        
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://chiragjain.gitbook.io/neetcode/112-path-sum/code.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
