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 79dfddd

Browse files
Update 0112.路径总和.md
增加112-python-简洁版解法
1 parent b81b392 commit 79dfddd

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 によって変換されたページ (->オリジナル) /