首页 > 其他 > 详细

129. 求根到叶子节点数字之和

时间:2020-08-30 17:30:11      阅读:71      评论:0      收藏:0      [点我收藏+]

技术分享图片
技术分享图片
技术分享图片

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

129. 求根到叶子节点数字之和

原文:https://www.cnblogs.com/panweiwei/p/13585721.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!