leetcode——437. 路径总和 III

class Solution:
    def pathSum(self, root: TreeNode, sum: int) -> int:
        if not root:
            return 0
        def dfs(node,sums):
            left=right=0
            temp=[num+node.val for num in sums] + [node.val]
            if node.left:
                left=dfs(node.left,temp)
            if node.right:
                right=dfs(node.right,temp)
            return temp.count(sum)+left+right
        return dfs(root,[])
执行用时 :228 ms, 在所有 python3 提交中击败了86.15%的用户
内存消耗 :34.8 MB, 在所有 python3 提交中击败了7.82%的用户
 
这个做法只是能看懂,自己却做不出来。。。
——2019.11.21
posted @ 2019-11-21 19:52  欣姐姐  阅读(152)  评论(0编辑  收藏  举报