![]()
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
import copy
class Solution:
# 返回二维列表,内部每个列表表示找到的路径
def FindPath(self, root, expectNumber):
# write code here
# 首先判断边界
if root == None:
return []
# 采用广度优先遍历我们的树,广度优先中有构造一个辅助列表
support = [root]
supportArray = [[root.val]]
retArray = []
while support:
temp = support[0]
tempsupportArray = supportArray[0]
# 需要判断当前节点是否为叶子节点
if temp.left==None and temp.right==None:
tempSum = sum(tempsupportArray)
if tempSum==expectNumber:
retArray.insert(0,tempsupportArray)
if temp.left:
support.append(temp.left)
#这里为啥用copy????
newtempsupportArray = copy.copy(tempsupportArray)
newtempsupportArray.append(temp.left.val)
supportArray.append(newtempsupportArray)
if temp.right:
support.append(temp.right)
newtempsupportArray = copy.copy(tempsupportArray)
newtempsupportArray.append(temp.right.val)
supportArray.append(newtempsupportArray)
del supportArray[0]
del support[0]
return retArray