From 428a93566c6fc52e8dfd03827e5b8f116ff748a8 Mon Sep 17 00:00:00 2001 From: ryosuketc <43229670+ryosuketc@users.noreply.github.com> Date: Mon, 17 Nov 2025 06:43:28 +0900 Subject: [PATCH] 104. Maximum Depth of Binary Tree https://leetcode.com/problems/maximum-depth-of-binary-tree/ --- 104_maximum_depth_of_binary_tree/memo.md | 22 +++++++++++++++ 104_maximum_depth_of_binary_tree/step1.cpp | 20 +++++++++++++ 104_maximum_depth_of_binary_tree/step2.cpp | 33 ++++++++++++++++++++++ 104_maximum_depth_of_binary_tree/step3.cpp | 0 4 files changed, 75 insertions(+) create mode 100644 104_maximum_depth_of_binary_tree/memo.md create mode 100644 104_maximum_depth_of_binary_tree/step1.cpp create mode 100644 104_maximum_depth_of_binary_tree/step2.cpp create mode 100644 104_maximum_depth_of_binary_tree/step3.cpp diff --git a/104_maximum_depth_of_binary_tree/memo.md b/104_maximum_depth_of_binary_tree/memo.md new file mode 100644 index 0000000..4513161 --- /dev/null +++ b/104_maximum_depth_of_binary_tree/memo.md @@ -0,0 +1,22 @@ +# 104. Maximum Depth of Binary Tree + +https://leetcode.com/problems/maximum-depth-of-binary-tree/ + +## Comments + +### step1 + +* まあ再帰で書こうかな、と思って 4:00 で AC +* `root` みたいな引数の関数を使うとなんか認知不可が高い気がする (`root` は木全体の根であって、再帰それぞれの根であるという感覚がないからな気がする) + +### step2 + +* 引数名を `node` (`root` ではなく) にしたかったので、`GetDepth` のような private 関数を使おうかと思ったが、結局 depth って定義上はより深い方を返す (そうでないなら、右と左の pair を返すとかはできるけど) ということになり、そうすると `maxDepth` と全く同じ機能になるので分ける必要もない気がした。 +* 一方でまあ深さを追加の引数として取って再帰するなら別の関数にする意味はある +* Arai60 で解いている + * https://github.com/ryosuketc/leetcode_arai60/pull/21/files + * 再帰のスタックオーバーフローとかを気にするなら、上記の通り iterative な解き方もあるかもしれない。上記は Python で書いたが、C++ なら `step2.Solution` みたいな感じかな。 + +### step3 + +* 省略 diff --git a/104_maximum_depth_of_binary_tree/step1.cpp b/104_maximum_depth_of_binary_tree/step1.cpp new file mode 100644 index 0000000..929167f --- /dev/null +++ b/104_maximum_depth_of_binary_tree/step1.cpp @@ -0,0 +1,20 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + int maxDepth(TreeNode* root) { + if (!root) { + return 0; + } + return std::max(maxDepth(root->left), maxDepth(root->right)) + 1; + } +}; diff --git a/104_maximum_depth_of_binary_tree/step2.cpp b/104_maximum_depth_of_binary_tree/step2.cpp new file mode 100644 index 0000000..50dd3ec --- /dev/null +++ b/104_maximum_depth_of_binary_tree/step2.cpp @@ -0,0 +1,33 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ + +#include + +class Solution { +public: + int maxDepth(TreeNode* root) { + std::stack> node_and_depth; + node_and_depth.push(std::make_pair(root, 1)); + int max_depth = 0; + while (!node_and_depth.empty()) { + auto [node, depth] = node_and_depth.top(); + node_and_depth.pop(); + if (!node) { + continue; + } + max_depth = std::max(max_depth, depth); + node_and_depth.push(std::make_pair(node->left, depth + 1)); + node_and_depth.push(std::make_pair(node->right, depth + 1)); + } + return max_depth; + } +}; diff --git a/104_maximum_depth_of_binary_tree/step3.cpp b/104_maximum_depth_of_binary_tree/step3.cpp new file mode 100644 index 0000000..e69de29