An input binary tree root node and an integer, to print out all the paths of the binary tree and the node value is an integer input. A path definition as the start path down to the known leaf node through the tree from the root node to be formed. Return value in the list, a large array Array front.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
import copy
class Solution:
def FindPath(self, root, expectNumber):
if root == None:
return []
ret = []
supportArrayList = [[root.val]] # 对于每个节点来说,保存其路径,保存的是node的值
support = [root] # 当前的node,保存的是node,用来做广度优先遍历
while support:
tmpNode = support[0]
tmpArrayList = supportArrayList[0]
if tmpNode.left == None and tmpNode.right == None:
if sum(tmpArrayList) == expectNumber:
ret.insert(0, tmpArrayList)
if tmpNode.left:
support.append(tmpNode.left)
newTmpArrayList = copy.copy(tmpArrayList)
newTmpArrayList.append(tmpNode.left.val)
supportArrayList.append(newTmpArrayList)
if tmpNode.right:
support.append(tmpNode.right)
newTmpArrayList = copy.copy(tmpArrayList)
newTmpArrayList.append(newTmpArrayList)
supportArrayList.append(newTmpArrayList)
del supportArrayList[0]
del support[0]
return ret