---
# System prepended metadata

title: Path Sum
tags: [data_structure_python]

---

---
tags: data_structure_python
---

# Path Sum

https://leetcode.com/problems/path-sum/

```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 root == None:
            return False
        
        left = self.hasPathSum(root.left, targetSum - root.val)
        right = self.hasPathSum(root.right, targetSum - root.val)
        
        if root.left == None and root.right == None and targetSum == root.val:
            return True
        
        return left or right
```