1 class Solution: 2 def __init__(self): 3 self.lists = [] 4 5 def preOrder(self,root,path,sums,target): 6 if root != None: 7 path.append(root.val) 8 sums += root.val 9 if root.left != None: 10 self.preOrder(root.left,path,sums,target) 11 if root.right != None: 12 self.preOrder(root.right,path,sums,target) 13 14 if root.left == None and root.right == None: 15 if sums == target: 16 self.lists.append(path[:]) 17 path.pop(-1) 18 sums -= target 19 20 def pathSum(self, root: TreeNode, sum: int) -> 'List[List[int]]': 21 self.preOrder(root,[],0,sum) 22 return self.lists
本题是leetcode112的升级版,需要检查多条路径,并保存。