- 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
来源: http://www.bubuko.com/infodetail-3297966.html