Merge branch 'youngyangyang04:master' into master

This commit is contained in:
Jeremy Feng 2023-04-01 08:39:10 +08:00 committed by GitHub
commit 619795cda2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 0 deletions

View File

@ -475,6 +475,12 @@ class solution:
return false # 别忘记处理空treenode
else:
return isornot(root, targetsum - root.val)
class Solution: # 简洁版
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
if not root: return False
if root.left==root.right==None and root.val == targetSum: return True
return self.hasPathSum(root.left,targetSum-root.val) or self.hasPathSum(root.right,targetSum-root.val)
```
**迭代 - 层序遍历**