From 1b8962479ba785a16c947ea2297a492cd30af306 Mon Sep 17 00:00:00 2001 From: Masakuni Date: Wed, 28 Jan 2026 19:17:43 +0900 Subject: [PATCH 1/3] step1 --- .../111.minimum-depth-of-binary-tree/memo.md | 7 ++++ .../111.minimum-depth-of-binary-tree/step1.py | 36 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 problems/111.minimum-depth-of-binary-tree/memo.md create mode 100644 problems/111.minimum-depth-of-binary-tree/step1.py diff --git a/problems/111.minimum-depth-of-binary-tree/memo.md b/problems/111.minimum-depth-of-binary-tree/memo.md new file mode 100644 index 0000000..69fafea --- /dev/null +++ b/problems/111.minimum-depth-of-binary-tree/memo.md @@ -0,0 +1,7 @@ +## step1 +- 最小の深さを求める +- 短い箇所を見つけるので幅優先探索の方がいい +- 片方だけNoneを見つけてWAを出してしまったので要件をしっかり確認するようにしたい +- 時間計算量はO(N) +- 空間計算量はO(logN)だと思う + diff --git a/problems/111.minimum-depth-of-binary-tree/step1.py b/problems/111.minimum-depth-of-binary-tree/step1.py new file mode 100644 index 0000000..f27b553 --- /dev/null +++ b/problems/111.minimum-depth-of-binary-tree/step1.py @@ -0,0 +1,36 @@ +# +# @lc app=leetcode id=111 lang=python3 +# +# [111] Minimum Depth of Binary Tree +# + +# @lc code=start +# 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 + depth = 1 + + to_visit = [root] + + while to_visit: + next_to_visit = [] + for node in to_visit: + if node.left is None and node.right is None: + return depth + if node.left is not None: + next_to_visit.append(node.left) + if node.right is not None: + next_to_visit.append(node.right) + + to_visit = next_to_visit + depth += 1 + + +# @lc code=end From eed984baf6bb12103f981b6be933f96588078de3 Mon Sep 17 00:00:00 2001 From: Masakuni Date: Wed, 28 Jan 2026 19:17:59 +0900 Subject: [PATCH 2/3] step2 --- .../111.minimum-depth-of-binary-tree/memo.md | 39 +++++++++++++++++++ .../111.minimum-depth-of-binary-tree/step2.py | 32 +++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 problems/111.minimum-depth-of-binary-tree/step2.py diff --git a/problems/111.minimum-depth-of-binary-tree/memo.md b/problems/111.minimum-depth-of-binary-tree/memo.md index 69fafea..9e49183 100644 --- a/problems/111.minimum-depth-of-binary-tree/memo.md +++ b/problems/111.minimum-depth-of-binary-tree/memo.md @@ -5,3 +5,42 @@ - 時間計算量はO(N) - 空間計算量はO(logN)だと思う +## step2 +- 他の人のコードを見てみる +- https://github.com/Yuto729/LeetCode_arai60/pull/27 + - `raise AssertionError("unreachable")`returnせずにwhileループを抜けたときに処理が何もないのは気持ちが悪いと思ったので一応入れておいた + - この気持ち悪さは自分の中にあった + - `is_leaf`は端的に状況を表していていいと思った + - 再帰でも書けるのは思いつかなかった + - じっくり見てやっと分かるくらい + - どのくらいStackに積まれるのかの見積もりが難しいが多分木の深さ + - 下記のような状態の時に無駄に探索されているように思った + - 結局全てのパターンで探索されているので無駄が多いと思った +``` + root + / \ + L R + / \ + L (leaf) + / + L + / + L + / + (leaf) +``` +- https://github.com/mamo3gr/arai60/pull/20/changes + - `入出力が合うかのガチャをやり始めると意識が離れてしまう。`これは難しい問題をバグらせた時についやってしまう。実務だと使えない方法なのでしないようにしたい。 + - 再帰のメリット、デメリットがまとめられていてよかった。分割統治法の意識があれば少し苦手意識が薄れそう。 + - `next_to_visitは辞書やhashの名前のような印象を受けました。`dict以外でxx_to_xxの変数名をつけないように意識づけたい +- https://github.com/plushn/SWE-Arai60/pull/22/changes + - `これも考慮する場合1つはmin_depth = float("inf")とすれば常に正しく動きます。もう一つは、想定を超える入力が来たときにはエラーを出して止めたりロガーで警告を出すなどのアプローチも考えられます。`後半の選択肢は持っていなかったので覚えておきたい。自分が書いたコードが部分的に変更される可能性を考えて問題が生じそうであれば何らかの形で通知する機能を作る選択肢を持っておく。 +- https://github.com/naoto-iwase/leetcode/pull/21/changes + - `これは私の趣味の問題かと思いますが、queue に複数のレベルのものが入るのはあまり好きではないです。`これは同じ感覚があり、forで全て見て次のものは別の変数にした +- https://github.com/nanae772/leetcode-arai60/pull/22/changes + - nodeとdepthを一緒に管理する方法も選択肢にあったが思い返すとメリットデメリットまでしっかり比較せずに、慣れてそうな方法で書いてしまった + - 一緒に更新するメリットはdepthの更新タイミングがとてもしっくりくること? + - デメリットは配列の中でnodeとdepthを管理するので少し構造が複雑になること + - 個人的にはちょうどどっちでも良いくらいだと思った + +- 再帰でも練習する diff --git a/problems/111.minimum-depth-of-binary-tree/step2.py b/problems/111.minimum-depth-of-binary-tree/step2.py new file mode 100644 index 0000000..3dedfeb --- /dev/null +++ b/problems/111.minimum-depth-of-binary-tree/step2.py @@ -0,0 +1,32 @@ +# +# @lc app=leetcode id=111 lang=python3 +# +# [111] Minimum Depth of Binary Tree +# + +# @lc code=start +# 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 + + def min_depth_helper(root: TreeNode) -> int: + if root.left is None and root.right is None: + return 1 + if root.left is None: + return min_depth_helper(root.right) + 1 + if root.right is None: + return min_depth_helper(root.left) + 1 + + return min(min_depth_helper(root.left), min_depth_helper(root.right)) + 1 + + return min_depth_helper(root) + + +# @lc code=end From a71174f6aab74736575c8cdb105fef36fa610bf7 Mon Sep 17 00:00:00 2001 From: Masakuni Date: Wed, 28 Jan 2026 19:18:11 +0900 Subject: [PATCH 3/3] step3 --- .../111.minimum-depth-of-binary-tree/memo.md | 5 +++ .../111.minimum-depth-of-binary-tree/step3.py | 40 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 problems/111.minimum-depth-of-binary-tree/step3.py diff --git a/problems/111.minimum-depth-of-binary-tree/memo.md b/problems/111.minimum-depth-of-binary-tree/memo.md index 9e49183..76df6ab 100644 --- a/problems/111.minimum-depth-of-binary-tree/memo.md +++ b/problems/111.minimum-depth-of-binary-tree/memo.md @@ -44,3 +44,8 @@ - 個人的にはちょうどどっちでも良いくらいだと思った - 再帰でも練習する + + +## step3 +- depthとnodeとpairで管理する方法でも書いた + diff --git a/problems/111.minimum-depth-of-binary-tree/step3.py b/problems/111.minimum-depth-of-binary-tree/step3.py new file mode 100644 index 0000000..fc8c47b --- /dev/null +++ b/problems/111.minimum-depth-of-binary-tree/step3.py @@ -0,0 +1,40 @@ +# +# @lc app=leetcode id=111 lang=python3 +# +# [111] Minimum Depth of Binary Tree +# + +# @lc code=start +# 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 +import collections + + +class Solution: + def minDepth(self, root: Optional[TreeNode]) -> int: + if root is None: + return 0 + + frontier = collections.deque([(root, 1)]) + while frontier: + + def is_leaf(node: TreeNode) -> bool: + return node.left is None and node.right is None + + node, depth = frontier.popleft() + if is_leaf(node): + return depth + + if node.left is not None: + frontier.append((node.left, depth + 1)) + if node.right is not None: + frontier.append((node.right, depth + 1)) + + raise AssertionError("unreachable") + + +# @lc code=end