@Lin--
2020-02-20T03:44:11.000000Z
字数 630
阅读 446
Leetcode
变式的路径输出问题
'''# File: hasPathSum.py# Author: 0HP# Date: 20200218# Purpose: solve the problem in website: https://leetcode.com/problems/path-sum/'''# Definition for a binary tree node.class TreeNode:def __init__(self, x):self.val = xself.left = Noneself.right = Noneclass Solution:def RecordPath(self,root:TreeNode,sum:int,sumlist:list):if root==None:returnsum=sum+root.valif root.left==None and root.right==None:sumlist.append(sum)self.RecordPath(root.left,sum,sumlist)self.RecordPath(root.right,sum,sumlist)def hasPathSum(self, root: TreeNode, sum: int) -> bool:Sum=0Sumlist=[]self.RecordPath(root,Sum,Sumlist)for i in sumlist:if sum==i:return Truereturn False