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