Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f0db060

Browse files
Merge pull request youngyangyang04#1959 from ZerenZhang2022/patch-16
Update 0112.路径总和.md
2 parents 5c0e98e + 79dfddd commit f0db060

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

‎problems/0112.路径总和.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,12 @@ class solution:
475475
return false # 别忘记处理空treenode
476476
else:
477477
return isornot(root, targetsum - root.val)
478+
479+
class Solution: # 简洁版
480+
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
481+
if not root: return False
482+
if root.left==root.right==None and root.val == targetSum: return True
483+
return self.hasPathSum(root.left,targetSum-root.val) or self.hasPathSum(root.right,targetSum-root.val)
478484
```
479485

480486
**迭代 - 层序遍历**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /