129. Sum Root to Leaf Numbers

129. Sum Root to Leaf Numbers

請參考 257. Binary Tree Paths

# 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