From 19b7bd0cb4599110fb30d18192c22c602b154f64 Mon Sep 17 00:00:00 2001 From: Hiro Funatsuka Date: Mon, 27 Apr 2026 23:37:59 +0900 Subject: [PATCH] Solve path sum --- README.md | 2 +- step1.md | 29 ++++++++++++++++++++++++++-- step2.md | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ step3.md | 10 +++++++--- 4 files changed, 91 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0ee68b0..24302a2 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # LeetCode -## 問題: [Link Text](URL) +## 問題: [112. Path Sum](https://leetcode.com/problems/path-sum/description/) ## 前提 diff --git a/step1.md b/step1.md index 54ebbc1..9c2462e 100644 --- a/step1.md +++ b/step1.md @@ -1,7 +1,32 @@ # Step 1 +- recursionで試したが、上手くいかなかったのでまずiterativeで試す + ```python +# 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 + + stack = [(root, root.val)] + while stack: + node, current_sum = stack.pop() + if node: + if node.left is None and node.right is None and current_sum == targetSum: + return True + if node.left is not None: + stack.append((node.left, current_sum + node.left.val)) + if node.right is not None: + stack.append((node.right, current_sum + node.right.val)) + return False ``` -時間計算量: -空間計算量: +時間計算量: O(n) + +空間計算量: O(n) diff --git a/step2.md b/step2.md index 6ec3360..2996861 100644 --- a/step2.md +++ b/step2.md @@ -1,4 +1,60 @@ # Step 2 +## Iterative DFS + +```python +# 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 + + nodes_and_sums = [(root, root.val)] + while nodes_and_sums: + node, current_sum = nodes_and_sums.pop() + if node: + if node.left is None and node.right is None and current_sum == targetSum: + return True + if node.left is not None: + nodes_and_sums.append((node.left, current_sum + node.left.val)) + if node.right is not None: + nodes_and_sums.append((node.right, current_sum + node.right.val)) + return False +``` + +時間計算量: O(n) + +空間計算量: O(n) + +## Recursive DFS + +- 他の人のコードを拝見 + ```python +# 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: + def dfs(node, current_sum): + if node is None: + return False + + current_sum += node.val + if node.left is None and node.right is None: + return current_sum == targetSum + return dfs(node.left, current_sum) or dfs(node.right, current_sum) + return dfs(root, 0) ``` + +時間計算量: O(n) + +空間計算量: O(n) diff --git a/step3.md b/step3.md index 522daa6..87a6c88 100644 --- a/step3.md +++ b/step3.md @@ -1,9 +1,13 @@ # Step 3 +- 思いつかなかったRecursive DFSで練習 + ```python + ``` -1回目: 分 秒 -2回目: 分 秒 +1回目: 1分 24秒 + +2回目: 1分 30秒 -3回目: 分 秒 +3回目: 1分 18秒