mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #1959 from ZerenZhang2022/patch-16
Update 0112.路径总和.md
This commit is contained in:
@ -475,6 +475,12 @@ class solution:
|
|||||||
return false # 别忘记处理空treenode
|
return false # 别忘记处理空treenode
|
||||||
else:
|
else:
|
||||||
return isornot(root, targetsum - root.val)
|
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)
|
||||||
```
|
```
|
||||||
|
|
||||||
**迭代 - 层序遍历**
|
**迭代 - 层序遍历**
|
||||||
|
Reference in New Issue
Block a user