From 340ef010f8e2a78173d6032d5c0b66c2393fcd4c Mon Sep 17 00:00:00 2001 From: Hiro Funatsuka Date: Sat, 25 Apr 2026 22:20:33 +0900 Subject: [PATCH] Solve minimum depth of binary tree --- README.md | 2 +- step1.md | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- step2.md | 22 ++++++++++++++++++++++ step3.md | 27 ++++++++++++++++++++++++--- 4 files changed, 101 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0ee68b0..0b0055c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # LeetCode -## 問題: [Link Text](URL) +## 問題: [111. Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/description/) ## 前提 diff --git a/step1.md b/step1.md index 54ebbc1..3d5dd9f 100644 --- a/step1.md +++ b/step1.md @@ -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 + stack = [[root, 1]] + 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) diff --git a/step2.md b/step2.md index 6ec3360..ab54427 100644 --- a/step2.md +++ b/step2.md @@ -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 ``` diff --git a/step3.md b/step3.md index 522daa6..9849ce4 100644 --- a/step3.md +++ b/step3.md @@ -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 ``` -1回目: 分 秒 +1回目: 2分 11秒 -2回目: 分 秒 +2回目: 2分 6秒 -3回目: 分 秒 +3回目: 2分 8秒