From ed03bffe6017e9030f268847eb9b7b452307df6a Mon Sep 17 00:00:00 2001 From: Rytxxx Date: Mon, 12 Jan 2026 01:15:22 -0800 Subject: [PATCH] 112-path-sum --- 112-path-sum/memo.md | 28 ++++++++++++++++++++++++++++ 112-path-sum/step1.py | 24 ++++++++++++++++++++++++ 112-path-sum/step2.py | 31 +++++++++++++++++++++++++++++++ 112-path-sum/step3.py | 18 ++++++++++++++++++ 4 files changed, 101 insertions(+) create mode 100644 112-path-sum/memo.md create mode 100644 112-path-sum/step1.py create mode 100644 112-path-sum/step2.py create mode 100644 112-path-sum/step3.py diff --git a/112-path-sum/memo.md b/112-path-sum/memo.md new file mode 100644 index 0000000..049c726 --- /dev/null +++ b/112-path-sum/memo.md @@ -0,0 +1,28 @@ +## step1で考えたこと + +Sumをどうやって渡していくか?関数呼び出しの時に渡すのが良さそう。となるとHelper関数が必要か + +self.checkPathSum(root, current_sum)を呼ぶタイミングがどこが適切かがわからない。今の場合だとまだ定義されていないから呼べないよと怒られる。 + +hasTarget = (current_sum == turgetSum) + +この部分で、三項演算子的なものを使って書きたかったがわからなかった。文法的にあっているかもわからない。 + +## step1へのフィードバック +内側関数はself不要 + +leafで判定するとより良い¥ + +合計を渡す代わりに残りのtargetを減らすやり方の方が賢かった + +### 参考にした他の人のコード + +#### https://github.com/mamo3gr/arai60/pull/24/ +再帰の考え方は同じだったが、Queueを使って書くという新しい発想が得られた。 +is_leafの議論も面白い。自分も欲しい派 + +Queueを使ったコードも読みやすい印象を持った。 + +#### https://github.com/plushn/SWE-Arai60/pull/25/ + +減法を使って書いていたコード、ほとんど同じ \ No newline at end of file diff --git a/112-path-sum/step1.py b/112-path-sum/step1.py new file mode 100644 index 0000000..4783b75 --- /dev/null +++ b/112-path-sum/step1.py @@ -0,0 +1,24 @@ +# 動かないコード + +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool: + hasTarget = False + current_sum = 0 + self.checkPathSum(root, current_sum) + + def checkPathSum(self, node: Optional[TreeNode], current_sum: int): + # leafまで辿り着いた時がnode is None、この時のcurrent_sumがtargetSumと一致しているかをチェック + if node is None: + hasTarget = (current_sum == targetSum) + else: + current_sum += int(node.val) + self.checkPathSum(node.left, current_sum) + self.checkPathSum(node.right, current_sum) + + return hasTarget diff --git a/112-path-sum/step2.py b/112-path-sum/step2.py new file mode 100644 index 0000000..0c3b99c --- /dev/null +++ b/112-path-sum/step2.py @@ -0,0 +1,31 @@ +# step1を修正したVersion +# Time Complexity: O(n) +# Space Complexity: O(h) + +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool: + hasTarget = False + + def checkPathSum(node: Optional[TreeNode], current_sum: int): + nonlocal hasTarget + if node is None or hasTarget: + return + + current_sum += node.val + + if node.left is None and node.right is None: + if current_sum == targetSum: + hasTarget = True + return + + checkPathSum(node.left, current_sum) + checkPathSum(node.right, current_sum) + + checkPathSum(root, 0) + return hasTarget diff --git a/112-path-sum/step3.py b/112-path-sum/step3.py new file mode 100644 index 0000000..a71850d --- /dev/null +++ b/112-path-sum/step3.py @@ -0,0 +1,18 @@ +# 個人的に落ち着いた回答のコード + +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool: + if root is None: + return False + + if root.left is None and root.right is None: + return root.val == targetSum + + return (self.hasPathSum(root.left, targetSum - root.val) or + self.hasPathSum(root.right, targetSum - root.val))