---
title: "129. Sum Root to Leaf Numbers"
url: "https://laigary.com/interview/coding/129-sum-root-to-leaf-numbers"
type: "note"
section: "coding"
date: "2024-07-21"
updated: "2024-07-21"
tags: ["Tree", "Backtrack"]
---

# 129. Sum Root to Leaf Numbers

[129\. Sum Root to Leaf Numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers/)

請參考 [257\. Binary Tree Paths](/interview/coding/257-binary-tree-paths)

```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 __init__(self):
        self.res = 0
    def sumNumbers(self, root: Optional[TreeNode]) -> int:

        self.res = 0

        def traverse(node, curr):
            if not node:
                return
            if not node.left and not node.right:
                curr = curr * 10 + node.val
                self.res += curr
                curr = curr // 10
                return
            curr = curr * 10 + node.val
            traverse(node.left, curr)
            traverse(node.right, curr)
            curr = curr // 10

        traverse(root, 0)

        return self.res
```
