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)
## 問題: [111. Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/description/)

## 前提

Expand Down
56 changes: 54 additions & 2 deletions step1.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,59 @@
# Step 1

## Recursive DFS (失敗)

- nodeがNoneだったら0を返す
- 左右最小の深さに1を足して返す

```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 minDepth(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0
return 1 + min(self.minDepth(root.left), self.minDepth(root.right))
```

- 葉が片方にしかないとき、rootに0が返ってくる

時間計算量: O(n)

空間計算量: O(n)

## Iterative DFS

- nodeはNoneではないが、left, rightがNoneの場合に最小の深さを更新する

```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 minDepth(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0

res = 10 ** 5
Copy link
Copy Markdown

@h-masder h-masder Apr 25, 2026

Choose a reason for hiding this comment

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

resの値は、将来変更される可能性が高いと思います。
どんな変数なのか、コメントアウトしておくと親切かと思います。

stack = [[root, 1]]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

変数名にはどんなものが入っているかを示したほうが分かりやすいかなと思います。
node_and_depthなどはいかがでしょうか。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

stackの変数名どうするか悩みました。

変数の中身が何かわかりやすくする場合、node_and_depthが良いと思います。他方で今回の処理がDFSであることをわかりやすくするためにはstackにしておいた方が良いとも思いました。

変数の中身のわかりやすさか処理のわかりやすさ(今回の場合DFSをしているとわかりやすく示すこと)のどちらを優先するかだと思います。

while stack:
node, depth = stack.pop()
if node:
if node.left is None and node.right is None:
res = min(res, depth)
else:
stack.append([node.left, depth + 1])
stack.append([node.right, depth + 1])
return res
```
時間計算量:

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

空間計算量: O(n)
22 changes: 22 additions & 0 deletions step2.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
# Step 2

```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 minDepth(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0

min_depth = float("inf")
stack = [(root, 1)]
while stack:
node, depth = stack.pop()
if node:
if node.left is None and node.right is None:
min_depth = min(res, depth)
else:
stack.append((node.left, depth + 1))
stack.append((node.right, depth + 1))
return min_depth
```
27 changes: 24 additions & 3 deletions step3.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
# Step 3

```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 minDepth(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0
min_depth = float("inf")
stack = [(root, 1)]
while stack:
node, depth = stack.pop()
if node:
if node.left is None and node.right is None:
min_depth = min(min_depth, depth)
else:
stack.append((node.left, depth + 1))
stack.append((node.right, depth + 1))
return min_depth
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

今回の問題はrootから最も近い葉までの距離を求める問題ですので、
近いものから順に探索していくほうが素直かと思います。
幅優先探索を使って解いてみてもいいかもしれないですね。

```
1回目: 分 秒
1回目: 2分 11秒

2回目: 分 秒
2回目: 2分 6秒

3回目: 分 秒
3回目: 2分 8秒