class Solution:
def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]:
res=[]
def traceback(node,trace,sum):
if not node:
return
if node.val==sum and (not node.left) and (not node.right):
res.append(trace+[node.val])
return
if node.left:
traceback(node.left,trace+[node.val],sum-node.val)
if node.right:
traceback(node.right,trace+[node.val],sum-node.val)
traceback(root,[],sum)
return res
class Solution:
def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]:
res, path = [], []
def dfs(node, sum):
#递归出口:解决子问题
if not node: return #如果没有节点(node = None),直接返回,不向下执行
else: #有节点
path.append(node.val) #将节点值添加到path
sum -= node.val
# 如果节点为叶子节点,并且 sum == 0
if not node.left and not node.right and not sum:
res.append(path[:])
dfs(node.left, sum) #递归处理左边
dfs(node.right, sum) #递归处理右边
path.pop() #处理完一个节点后,恢复初始状态,为node.left, node.right操作
dfs(root, sum)
return res