Skip to content

Latest commit

 

History

History
40 lines (29 loc) · 959 Bytes

113._path_sum_ii.md

File metadata and controls

40 lines (29 loc) · 959 Bytes

113. Path Sum II

题目:

https://leetcode.com/problems/path-sum-ii/

tag : DFS

难度 : Medium

注意宁愿写几次curList + [root.val] 也不要直接传一个list进去,因为list pass by reference的亏已经吃过了

class Solution(object):
    def pathSum(self, root, sum):
        """
        :type root: TreeNode
        :type sum: int
        :rtype: List[List[int]]
        """
        res = []
        self.auxPathSum(root, sum, [], res)
        return res
    def auxPathSum(self, root, sum, cur_list, cur_lists):
        if not root:
            return
        sum -= root.val
        if sum == 0 and not root.left and not root.right:
            cur_lists.append(cur_list + [root.val])
            return 
        if root.left:
            self.auxPathSum(root.left, sum, cur_list + [root.val], cur_lists) 
        if root.right:
            self.auxPathSum(root.right, sum, cur_list + [root.val], cur_lists)