【python/M/129】Sum Root to Leaf Numbers

题目

题目太长了,戳链接

实现代码

# 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)

猜你喜欢

转载自blog.csdn.net/alicelmx/article/details/81455754