24. A binary tree and path (Python) to a certain value

Title Description

Input nodes and a binary tree with an integer, the binary print values ​​of the nodes and the paths to all the input integers. Forming a path to the path definition begins from the root node of the tree down to the leaf node has been traversed nodes. (Note: the return value in the list, a large array Array front)
. 1  Import Copy
 2  class Solution:
 . 3      # return a two-dimensional list, each list showing the internal path found 
. 4      DEF FindPath (Self, the root, expectNumber):
 . 5          # Write code here Wallpaper 
. 6          IF the root == None:
 . 7              return []
 . 8          = Support [the root]
 . 9          supportArrayList = [[root.val]]
 10          RET = []
 . 11          the while Support:
 12 is              tmpnodes = Support [0]
 13 is              tmpArrayList = supportArrayList [0]
14             if tmpNode.left==None and tmpNode.right == None:
15                 if sum(tmpArrayList)==expectNumber:
16                     ret.insert(0,tmpArrayList)
17             if tmpNode.left:
18                 support.append(tmpNode.left)
19                 newtmpArrayList =copy.copy(tmpArrayList)
20                 newtmpArrayList.append(tmpNode.left.val)
21                 supportArrayList.append(newtmpArrayList)
22             if tmpNode.right:
23                 support.append(tmpNode.right)
24                 newtmpArrayList = copy.copy(tmpArrayList)
25                 newtmpArrayList.append(tmpNode.right.val)
26                 supportArrayList.append(newtmpArrayList)
27             del support[0]
28             del supportArrayList[0]
29         return ret

2019-12-15 09:28:59 

Guess you like

Origin www.cnblogs.com/NPC-assange/p/12042019.html