From 79dfddde054abed5c766aa70ed56f54ee3b1c3ef Mon Sep 17 00:00:00 2001 From: ZerenZhang2022 <118794589+ZerenZhang2022@users.noreply.github.com> Date: Sat, 18 Mar 2023 15:43:20 -0400 Subject: [PATCH] =?UTF-8?q?Update=200112.=E8=B7=AF=E5=BE=84=E6=80=BB?= =?UTF-8?q?=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加112-python-简洁版解法 --- problems/0112.路径总和.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index e412d38e..bb46f04a 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -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) ``` **迭代 - 层序遍历**