问题描述
https://leetcode.cn/problems/path-sum-ii/description/
解题思路
首先,我们设置一个容器存储最终的结果。
其次,我们在遍历过程中,更新数组。
然后,在叶子结点处判断是否加入。
代码
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]: res = [] def dfs(root, targetSum, tmp_li): t_copy = tmp_li[:] if root is None: return t_copy.append(root.val) if root.left is None and root.right is None and targetSum == root.val: res.append(t_copy) dfs(root.left, targetSum-root.val, t_copy) dfs(root.right, targetSum-root.val, t_copy) dfs(root, targetSum, []) return res
标签:路经,None,right,val,self,II,targetSum,113,root From: https://www.cnblogs.com/bjfu-vth/p/17072391.html