剑指offer-二叉树中和为某一值的路径24
题目描述
输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意: 在返回值的list中,数组长度大的数组靠前)
import copy class Solution: # 返回二维列表,内部每个列表表示找到的路径 def __init__(self): self.listAll=[] self.list=[] def FindPath(self, root, expectNumber): # write code here if(root==None): return self.listAll self.list.append(root.val) expectNumber-=root.val if expectNumber==0 and root.left==None and root.right==None: self.listAll.append(copy.deepcopy(self.list)) self.FindPath(root.left,expectNumber) self.FindPath(root.right,expectNumber) self.list.pop(-1) return self.listAll