首页 > 其他 > 详细

437. 路径总和 III

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

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

class Solution(object):
    def pathSum(self, root, target):
        """
        :type root: TreeNode
        :type sumt: int
        :rtype: int
        """
        self.count = 0
        myDict = {0: 1}

        def dfs(p, target, pathSum, myDict):
            if p:
                pathSum += p.val
                self.count += myDict.get(pathSum - target, 0)
                myDict[pathSum] = myDict.get(pathSum, 0) + 1
                dfs(p.left, target, pathSum, myDict)
                dfs(p.right, target, pathSum, myDict)
                myDict[pathSum] -= 1
        dfs(root, target, 0, myDict)
        return self.count

437. 路径总和 III

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

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