Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# LeetCode

## 問題: [Link Text](URL)
Copy link
Copy Markdown

@liquo-rice liquo-rice Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PRをマージしないにしても、テンプレフォルダーをコピーして、問題ごとにフォルダーを作ると、diffが綺麗に出て、(マージするなら)ローカルにファイルも残るので良いと思います。

## 問題: [112. Path Sum](https://leetcode.com/problems/path-sum/description/)

## 前提

Expand Down
29 changes: 27 additions & 2 deletions step1.md
Original file line number Diff line number Diff line change
@@ -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)]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらのコメントをご参照ください。
rimokem/arai60#25 (comment)

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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ほぼ同じですが、これでもいいかなと思いました。

class Solution:
    def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
        if root is None:
            return False

        stack = [(root, targetSum)]
        while stack:
            node, targetSum = stack.pop()
            if node:
                if node.left is None and node.right is None and node.val == targetSum:
                    return True
                if node.left is not None:
                    stack.append((node.left, targetSum - node.val))
                if node.right is not None:
                    stack.append((node.right, targetSum - node.val))
        return False

```
時間計算量:

空間計算量:
時間計算量: O(n)

空間計算量: O(n)
56 changes: 56 additions & 0 deletions step2.md
Original file line number Diff line number Diff line change
@@ -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:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

必要なさそうですかね?

必要だとしても、ネストを一段減らすために、if not node: continueの形にした方が良さそうです。

また、Noneかどうかのチェックの方法に一貫性がないのが気になります。

if node.left is None and node.right is None and current_sum == targetSum:
return True
Comment on lines +21 to +22
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

current_sum == targetSumのチェックはifの中に入れて、一致しないならcontinueする方が良いと思います。

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)
10 changes: 7 additions & 3 deletions step3.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# Step 3

- 思いつかなかったRecursive DFSで練習

```python

```
1回目: 分 秒

2回目: 分 秒
1回目: 1分 24秒

2回目: 1分 30秒

3回目: 分 秒
3回目: 1分 18秒