暴力法,注意列表的传递方式,精髓
# 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]]
"""
if not root:
return []
res = list()
def helper(list_,node_,sum_):
tmp_sum = sum_ + node_.val
if not node_.left and not node_.right:
if tmp_sum == sum:
list_ += [node_.val]
res.append(list_)
if node_.left:
helper(list_ + [node_.val],node_.left,tmp_sum)
if node_.right:
helper(list_ + [node_.val],node_.right,tmp_sum)
helper(list(),root,0)
return res