1 class Solution: 2 def __init__(self): 3 self.lists = [] 4 5 def preOrder(self,root,path): 6 if root != None: 7 path.append(str(root.val)) 8 if root.left != None: 9 self.preOrder(root.left,path) 10 if root.right != None: 11 self.preOrder(root.right,path) 12 13 if root.left == None and root.right == None: 14 self.lists.append(path[:]) 15 path.pop(-1) 16 17 def sumNumbers(self, root: TreeNode) -> int: 18 if root == None: 19 return 0 20 else: 21 self.preOrder(root,[]) 22 sums = 0 23 for li in self.lists: 24 s = ''.join(li) 25 n = int(s) 26 sums += n 27 return sums