113. Path Sum II
题目:
https://leetcode.com/problems/path-sum-ii/
tag : DFS
难度 : Medium
注意宁愿写几次curList + [root.val] 也不要直接传一个list进去,因为list pass by reference的亏已经吃过了
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def pathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: List[List[int]]
"""
result = []
self.auxPathSum(root,sum,[],result)
return result
def auxPathSum(self, root, sum, curList, curLists):
if root == None:
return
sum -= root.val
if sum == 0 and (root.left == None and root.right == None):
curLists.append(curList + [root.val])
return
if root.left != None:
self.auxPathSum(root.left, sum - root.val, curList + [root.val],curLists)
if root.right != None:
self.auxPathSum(root.right, sum - root.val, curList + [root.val],curLists)