题解 | #二叉树中和为某一值的路径(二)#
二叉树中和为某一值的路径(二)
http://www.nowcoder.com/practice/b736e784e3e34731af99065031301bca
class Solution:
def FindPath(self, root: TreeNode, target: int) -> list:
# write code here
self.final,self.f = [] , [] # 存放所有路径以及当前路径
self.find_root(root)
return [a for a in self.final if sum(a)== target] # 找出和刚好为target的列表
def find_root(self,node): # 找出所有到叶子节点的路径
if not node:
return
self.f.append(node.val)
self.find_root(node.left)
self.find_root(node.right)
if not node.left and not node.right:
self.final.append(self.f.copy())
self.f.pop()