---
# System prepended metadata

title: Sum Root to Leaf Numbers
tags: [data_structure_python]

---

---
tags: data_structure_python
---

# Sum Root to Leaf Numbers

https://leetcode.com/problems/sum-root-to-leaf-numbers/

```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 sumNumbers(self, root: Optional[TreeNode]) -> int:
        
        def dfs(node, path):
            path += str(node.val)
            
            if node.left: dfs(node.left, path)
            if node.right: dfs(node.right, path)
            
            if node.left == None and node.right == None:
                root_to_leaf_paths.append(path)            
            
                
        root_to_leaf_paths = []
        dfs(root, "")
        
        return sum([int(elt) for elt in root_to_leaf_paths])
```