129. 求根到叶子节点数字之和





class Solution(object):
    def sumNumbers(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root:
            return 0
        return self.dfs(root, 0)

    def dfs(self, root, sum):
        total = 0
        sum = sum*10 + root.val
        if not root.left and not root.right:
            return sum
        else:
            if root.left:
                total += self.dfs(root.left, sum)
            if root.right:
                total += self.dfs(root.right, sum)
            return total

原文地址:https://www.cnblogs.com/panweiwei/p/13585721.html