# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def sumNumbers(self, root):
"""
:type root: TreeNode
:rtype: int
"""
res = self.getNumbers(root)
sum = 0
for i in range(len(res)):
sum += self.realNumber(res[i])
return sum
def getNumbers(self,root):
res = []
if not root:
return res
if not root.left and not root.right:
res.append([root.val])
return res
leftPaths = self.getNumbers(root.left)
for i in range(len(leftPaths)):
res.append([root.val] + leftPaths[i])
rightPaths = self.getNumbers(root.right)
for i in range(len(rightPaths)):
res.append([root.val] + rightPaths[i])
return res
def realNumber(self,l):
realNum = 0
weight = 1
while l:
realNum += l.pop() * weight
weight = weight * 10
return realNum