题解 | #二叉树中和为某一值的路径(二)#
二叉树中和为某一值的路径(二)
https://www.nowcoder.com/practice/b736e784e3e34731af99065031301bca
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param root TreeNode类 # @param target int整型 # @return int整型二维数组 # class Solution: def FindPath(self, root: TreeNode, target: int) -> List[List[int]]: # write code here res, path = [], [] def dfs(root, target): # 处理空 if not root: return path.append(root.val) # 是否到叶子结点以及是否满足target条件 if not root.left and not root.right and target - root.val == 0: # 要的是链表不是地址 res.append(path[:]) dfs(root.left, target - root.val) dfs(root.right, target - root.val) # 不满足条件的结点被删除 比如案例中的4 path.pop() dfs(root,target) return res