From ee4d4ec7854e7ec3b53c7ec489a219046184f0d8 Mon Sep 17 00:00:00 2001 From: Keisuke KUDO <151166401+Apo-Matchbox@users.noreply.github.com> Date: Sun, 7 Dec 2025 17:41:02 +0900 Subject: [PATCH 1/2] Create 104. Maximum Depth of Binary Tree Added detailed problem analysis, solution approaches, and code examples for calculating the maximum depth of a binary tree. --- 104/104. Maximum Depth of Binary Tree.md | 149 +++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 104/104. Maximum Depth of Binary Tree.md diff --git a/104/104. Maximum Depth of Binary Tree.md b/104/104. Maximum Depth of Binary Tree.md new file mode 100644 index 0000000..dab93c6 --- /dev/null +++ b/104/104. Maximum Depth of Binary Tree.md @@ -0,0 +1,149 @@ +# [104. Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/description/) +## Step1 +### 問題意図の考察 +- 問題文の確認 + - 与えられた二分木の根に対して、最大深度を返す。 + - 二分木の最大深度 = root nodeから最も遠い leaf nodeまで + +- 制約の確認 + - nodeの数は、0 ~ 10^4 + - 各nodeの値は、-100 ~ 100 + +- 問題意図 + - nodeの数に注意 + - 左部分ぽい所の深さを求める + - 右部分ぽい所の深さを求める + - そのうち大きい方を返す + +### 解法を考える。 +- DFS + - 再帰で返す方法 シンプルで良さそう +- BFS + - 何段階のレベルがあるか分かればよい + - level1 -> level2 -> level3 + +初回の回答 +```cpp +#include + +class Solution { +public: + int maxDepth(TreeNode* root) { + if (root == nullptr) { + return 0; + } + + int left = maxDepth(root->left); + int right = maxDepth(root->right); + + return std::max(left, right) + 1; + } +}; + +``` + +### コメント +- TreeNode の定義内で、left, rightが使用されているので、命名が良くないかな。 + - left_depth, right_depthくらいが分かりやすいかもしれない +- if (root = nullptr) に関して + - https://github.com/nktr-cp/leetcode/pull/22/files#diff-d16010dc25fbb50a03680c436bbb0d90f462bf27b3aa26421f1df94d5f15aa15 + - 他の方のcodeで、if (!root) という記述があった。違いを少し考えた。 + - ポインタが null -> false -> true (if条件成立) + - root を nullptrと比較 + +```cpp +#include + +class Solution { +public: + int maxDepth(TreeNode* root) { + if (!root) { + return 0; + } + + int left_depth = maxDepth(root->left); + int right_depth = maxDepth(root->right); + + return std::max(left_depth, right_depth) + 1; + } +}; + +``` + +## Step2 *2段階目、自然な書き方を考えて整理する +- 改善点 + - https://github.com/nktr-cp/leetcode/pull/22/files#diff-d16010dc25fbb50a03680c436bbb0d90f462bf27b3aa26421f1df94d5f15aa15 + - もっとシンプルに書いている例があった。 +```cpp +class Solution { +public: + int maxDepth(TreeNode* root) { + if (root == nullptr) { + return 0; + } + return std::max(maxDepth(root->left), maxDepth(root->right)) + 1; + } +}; + +``` + +- 他の方のコードを読む + - 前述:https://github.com/nktr-cp/leetcode/pull/22/files + - https://github.com/5103246/LeetCode_Arai60/pull/20/commits/cb2a3f1f447cd59e8d6ac1e2910262a6a05ca7b4 + - https://github.com/5ky7/arai60/pull/20/commits/6c2084a4cf7e5760e2d5130518cda59496833be6 + - https://github.com/ryosuketc/leetcode_arai60/pull/21/commits/333e716a565c4c0cdb627e8d378410010fd64588 + - https://github.com/nanae772/leetcode-arai60/pull/21/commits/9cc3d36c42d10bef83b97d017a73b65d5c0e52af + > 木の高さと深さの違いがよく分かってなかったが + > nodeの高さ=nodeから最も遠い葉までのパスの長さ + > 木の高さ=rootから最も遠い葉までのパスの長さ + > nodeの深さ=nodeからrootまでのパスの長さ + - ここはしっかりと押さえておきたい + - (要復習) + - C/C++だとポインタを持てるのでそのポインタを通じて親への伝播が自動的に行える。 + - https://discord.com/channels/1084280443945353267/1227073733844406343/1236324993839792149 + +- BFS(Breadth-First Search)の書き方 + - 分からなかったので、参照 + - https://cp-algorithms.com/graph/breadth-first-search.html + - https://www.geeksforgeeks.org/dsa/breadth-first-search-or-bfs-for-a-graph/ + +```cpp +class Solution { + public: + int maxDepth(TreeNode* root) { + if (root == nullptr) { + return 0; + } + + std::queue q; + q.push(root); + + int depth = 0; + + while (!q.empty()) { + int level_size = q.size(); + ++depth; + + for (int i = 0; i < level_size; ++i) { + TreeNode* node = q.front(); + q.pop(); + if (node->left != nullptr) { + q.push(node->left); + } + if (node->right != nullptr) { + q.push(node->right); + } + } + } + return depth; + } +}; + +``` + +- トータルで1Qにかかる時間が短くなってきた。 + +## Step3 *自然な流れ(感覚に落とし込む) +1. 2 min +2. 2 min +2. 1min From 5bd38b2abf7c3c6b180a4d2d364fc01717d5e19d Mon Sep 17 00:00:00 2001 From: Keisuke KUDO <151166401+Apo-Matchbox@users.noreply.github.com> Date: Mon, 8 Dec 2025 21:39:21 +0900 Subject: [PATCH 2/2] Update 104/104. Maximum Depth of Binary Tree.md Co-authored-by: t9a-dev --- 104/104. Maximum Depth of Binary Tree.md | 1 + 1 file changed, 1 insertion(+) diff --git a/104/104. Maximum Depth of Binary Tree.md b/104/104. Maximum Depth of Binary Tree.md index dab93c6..955e4c3 100644 --- a/104/104. Maximum Depth of Binary Tree.md +++ b/104/104. Maximum Depth of Binary Tree.md @@ -135,6 +135,7 @@ class Solution { } } } + return depth; } };