标签:def node ber append one 二维 pre root none
1 import copy 2 class Solution: 3 # 返回二维列表,内部每个列表表示找到的路径 4 def FindPath(self, root, expectNumber): 5 # write code here 6 if root == None: 7 return [] 8 support = [root] 9 supportArrayList = [[root.val]] 10 ret=[] 11 while support: 12 tmpNode = support[0] 13 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
标签:def node ber append one 二维 pre root none
原文地址:https://www.cnblogs.com/NPC-assange/p/12042019.html