题目
题目太长了,戳链接吧
实现代码
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def sumNumbers(self, root):
"""
:type root: TreeNode
:rtype: int
"""
return self.sumN(root,0)
def sumN(self,root,preSum):
if root == None:return 0
preSum = preSum*10 + root.val
if root.left == None and root.right == None:
return preSum
return self.sumN(root.left,preSum) + self.sumN(root.right,preSum)