From a2a05e5bf5d608fc779696d3f404ba8ae01c453d Mon Sep 17 00:00:00 2001 From: Yota Maeda Date: Wed, 1 Jul 2026 23:36:21 +0900 Subject: [PATCH 1/3] step1,2,3 --- 104-Maximum-Depth-of-Binary-Tree/note.md | 25 +++++++++++++++++++ 104-Maximum-Depth-of-Binary-Tree/step1.py | 29 +++++++++++++++++++++++ 104-Maximum-Depth-of-Binary-Tree/step2.py | 15 ++++++++++++ 104-Maximum-Depth-of-Binary-Tree/step3.py | 29 +++++++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 104-Maximum-Depth-of-Binary-Tree/note.md create mode 100644 104-Maximum-Depth-of-Binary-Tree/step1.py create mode 100644 104-Maximum-Depth-of-Binary-Tree/step2.py create mode 100644 104-Maximum-Depth-of-Binary-Tree/step3.py diff --git a/104-Maximum-Depth-of-Binary-Tree/note.md b/104-Maximum-Depth-of-Binary-Tree/note.md new file mode 100644 index 0000000..df4b913 --- /dev/null +++ b/104-Maximum-Depth-of-Binary-Tree/note.md @@ -0,0 +1,25 @@ +# 104. Maximum Depth of Binary Tree + +https://leetcode.com/problems/maximum-depth-of-binary-tree/ + +## step1(まず通す) + +深さ優先探索だと一番深いところにいつ当たるかわからないので、max_depth を持っておいて更新していく。 + +幅優先探索だと木なら同じ高さのノードを順番に見ていく形になるので最後に見たノードが一番深い。 + +ということで幅優先探索する。いつも (node, distance) のキューでやってたが、今回は階層ごとに見ていって一番深いところの深さを得るというのを素朴に表現してみる。 + +## step2(整形&他の人のコードを読む) + +- https://github.com/hayashi-ay/leetcode/pull/22/changes + - Python では再帰が好まれない印象なので避けたが、木のような構造が再帰的なやつは再帰で書くととてもわかりやすい + - 浅井健一『プログラミングの基礎』(サイエンス社)(ちなみにこんな名前で OCaml。名著。)ではデータ構造の再帰的な定義と一対一で対応した再帰を「構造にしたがった再帰」と呼んでいるが、今回のようなものはまさにそう。 + - 木の再帰的な定義: + - None は木である + - ある一つのノードがあってその子が全て木であるものは木である。 + - この問題を解く再帰関数: + - None なら高さは 0 である + - ある一つのノードにおける高さはその子の高さの max に 1 を加えたものである。 + +## step3(10分以内にさっとかける \* 3回) diff --git a/104-Maximum-Depth-of-Binary-Tree/step1.py b/104-Maximum-Depth-of-Binary-Tree/step1.py new file mode 100644 index 0000000..26d0402 --- /dev/null +++ b/104-Maximum-Depth-of-Binary-Tree/step1.py @@ -0,0 +1,29 @@ +from typing import Optional + + +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + + +class Solution: + def maxDepth(self, root: Optional[TreeNode]) -> int: + if root is None: + return 0 + + nodes = [root] + next_layer = [] + depth = 0 + while nodes: + depth += 1 + for node in nodes: + if node.left is not None: + next_layer.append(node.left) + if node.right is not None: + next_layer.append(node.right) + nodes = next_layer + next_layer = [] + + return depth diff --git a/104-Maximum-Depth-of-Binary-Tree/step2.py b/104-Maximum-Depth-of-Binary-Tree/step2.py new file mode 100644 index 0000000..81eebcb --- /dev/null +++ b/104-Maximum-Depth-of-Binary-Tree/step2.py @@ -0,0 +1,15 @@ +from typing import Optional + + +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + + +class Solution: + def maxDepth(self, root: Optional[TreeNode]) -> int: + if root is None: + return 0 + return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1 diff --git a/104-Maximum-Depth-of-Binary-Tree/step3.py b/104-Maximum-Depth-of-Binary-Tree/step3.py new file mode 100644 index 0000000..26d0402 --- /dev/null +++ b/104-Maximum-Depth-of-Binary-Tree/step3.py @@ -0,0 +1,29 @@ +from typing import Optional + + +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + + +class Solution: + def maxDepth(self, root: Optional[TreeNode]) -> int: + if root is None: + return 0 + + nodes = [root] + next_layer = [] + depth = 0 + while nodes: + depth += 1 + for node in nodes: + if node.left is not None: + next_layer.append(node.left) + if node.right is not None: + next_layer.append(node.right) + nodes = next_layer + next_layer = [] + + return depth From df8e04f19193432baf71218bfeb41a22a484c01d Mon Sep 17 00:00:00 2001 From: Yota Maeda Date: Wed, 1 Jul 2026 23:40:31 +0900 Subject: [PATCH 2/3] Add comment --- 104-Maximum-Depth-of-Binary-Tree/note.md | 1 + 1 file changed, 1 insertion(+) diff --git a/104-Maximum-Depth-of-Binary-Tree/note.md b/104-Maximum-Depth-of-Binary-Tree/note.md index df4b913..c843b31 100644 --- a/104-Maximum-Depth-of-Binary-Tree/note.md +++ b/104-Maximum-Depth-of-Binary-Tree/note.md @@ -21,5 +21,6 @@ https://leetcode.com/problems/maximum-depth-of-binary-tree/ - この問題を解く再帰関数: - None なら高さは 0 である - ある一つのノードにおける高さはその子の高さの max に 1 を加えたものである。 + - 今回は ノード数 <= 10 ^ 4 なので、再帰の深さは log 10 ^ 4 ~ 13 とかなので Python のリミット的にもスタックのサイズ的にも基本的に問題ない。 ## step3(10分以内にさっとかける \* 3回) From c05b5340df66c78f157711438d714916b285737b Mon Sep 17 00:00:00 2001 From: Yota Maeda Date: Wed, 1 Jul 2026 23:41:10 +0900 Subject: [PATCH 3/3] add link --- 104-Maximum-Depth-of-Binary-Tree/note.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/104-Maximum-Depth-of-Binary-Tree/note.md b/104-Maximum-Depth-of-Binary-Tree/note.md index c843b31..66adbb4 100644 --- a/104-Maximum-Depth-of-Binary-Tree/note.md +++ b/104-Maximum-Depth-of-Binary-Tree/note.md @@ -14,7 +14,7 @@ https://leetcode.com/problems/maximum-depth-of-binary-tree/ - https://github.com/hayashi-ay/leetcode/pull/22/changes - Python では再帰が好まれない印象なので避けたが、木のような構造が再帰的なやつは再帰で書くととてもわかりやすい - - 浅井健一『プログラミングの基礎』(サイエンス社)(ちなみにこんな名前で OCaml。名著。)ではデータ構造の再帰的な定義と一対一で対応した再帰を「構造にしたがった再帰」と呼んでいるが、今回のようなものはまさにそう。 + - [浅井健一『プログラミングの基礎』(サイエンス社)](https://www.saiensu.co.jp/search/?isbn=978-4-7819-1160-1&y=2007#detail)(ちなみにこんな名前で OCaml。名著。)ではデータ構造の再帰的な定義と一対一で対応した再帰を「構造にしたがった再帰」と呼んでいるが、今回のようなものはまさにそう。 - 木の再帰的な定義: - None は木である - ある一つのノードがあってその子が全て木であるものは木である。