From e1d5ecf2a993a33296cc7acb51fd2ae5912286ca Mon Sep 17 00:00:00 2001 From: Ryotaro Kurita Date: Sat, 27 Jul 2024 21:00:40 +0900 Subject: [PATCH 1/3] finish problem21 --- .DS_Store | Bin 6148 -> 6148 bytes 104.MaximumDepthofBinaryTree/bfs.cpp | 40 +++++++++++++++++++++++++ 104.MaximumDepthofBinaryTree/memo.md | 40 +++++++++++++++++++++++++ 104.MaximumDepthofBinaryTree/step1.cpp | 28 +++++++++++++++++ 104.MaximumDepthofBinaryTree/step2.cpp | 31 +++++++++++++++++++ 104.MaximumDepthofBinaryTree/step3.cpp | 31 +++++++++++++++++++ 6 files changed, 170 insertions(+) create mode 100644 104.MaximumDepthofBinaryTree/bfs.cpp create mode 100644 104.MaximumDepthofBinaryTree/memo.md create mode 100644 104.MaximumDepthofBinaryTree/step1.cpp create mode 100644 104.MaximumDepthofBinaryTree/step2.cpp create mode 100644 104.MaximumDepthofBinaryTree/step3.cpp diff --git a/.DS_Store b/.DS_Store index 4f25a517e46cbb8f036c0edd522f5c6970b97182..3dc170b7da253fc1b1220cabeed0a86a3e3d2108 100644 GIT binary patch delta 151 zcmZoMXfc@JFUrBdz`)4BAi%)j%aF)W!H~(2%TT&mkYhP>AV^Av!H~g#!Gu8%SsJLs zg&~!pfT4sTgCUn$3_7#6Ktdu_{cq hVijg=-t5O}&IqL1*erq6 zB>l66QgN4=+4*PYpMO{0%FBoCBfxUjLmkU;QtGI2{LZ8fc$%)J_#mSuRT*J>iT?uJAC7 zq%qJ?W=9t`Pq<|&+$Yw;`@dw|Y{_<9Eiv`PXL!)W2YoBaw_h4)J{BseVrl- candidates; + candidates.push({root, 1}); + int max_depth = 0; + while (!candidates.empty()) { + const auto [node, depth] = candidates.front(); + candidates.pop(); + if (node == nullptr) { + continue; + } + max_depth = max(depth, max_depth); + candidates.push({node->left, depth + 1}); + candidates.push({node->right, depth + 1}); + } + + return max_depth; + } +}; diff --git a/104.MaximumDepthofBinaryTree/memo.md b/104.MaximumDepthofBinaryTree/memo.md new file mode 100644 index 0000000..a01801a --- /dev/null +++ b/104.MaximumDepthofBinaryTree/memo.md @@ -0,0 +1,40 @@ +## ステップ1 +階層が深くなるごとに数値を足していって、再帰でnullptrになるまで潜れば解けそう +関数化したけどこの長さなら関数化する必要ないかもだけど +もっと複雑化することを想定して今回は分けてみた + +時間計算量 +深さに影響され、全て左に紐づく場合を考慮してO(n) + +空間計算量 +O(n) + +## ステップ2 +rootがnullptrとなっている場合を考慮してstep2で追加 + +return 1+max(maxDepth(root->left),maxDepth(root->right));のようにも書けるが +若干可読性が下がると感じた。 +https://leetcode.com/problems/maximum-depth-of-binary-tree/solutions/3192154/cpp-solve-easy-solve/ + +## 他の解法 +・BFSを使っても解くことができる bfs.cppに実装 +structで深さとnodeを持つ構造を定義 +命名ルールは下記参照 +https://google.github.io/styleguide/cppguide.html#Type_Names + +参考にした回答 +同階層をforループで回すよりは、階層ごとの深さをもつ方が良さそう +https://github.com/rossy0213/leetcode/pull/10/commits/827bf9f2ed20d4c647561fe9a0d73b761d04564e + +popしてからnodeを確認する方法がある。 +https://github.com/NobukiFukui/Grind75-ProgrammingTraining/pull/38 +これをすることで左と右それぞれ確認してからqueueに追加する必要がなくなり +シンプルに書くことができる + +・「この環境では大丈夫」なコードは可能ならば避ける +https://github.com/kazukiii/leetcode/pull/22/commits/f2d8eedba1e98a8da5c97093224cca35326e7cca#diff-e6fac96b6faeb56aacc2da73ab20276cbd0c1c118797522d3a88bd5d271b384c + +https://github.com/rossy0213/leetcode/pull/10/commits/827bf9f2ed20d4c647561fe9a0d73b761d04564e + +## Discorなど + diff --git a/104.MaximumDepthofBinaryTree/step1.cpp b/104.MaximumDepthofBinaryTree/step1.cpp new file mode 100644 index 0000000..4af98eb --- /dev/null +++ b/104.MaximumDepthofBinaryTree/step1.cpp @@ -0,0 +1,28 @@ +/** + * 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 MeasureDepth(TreeNode* node) { + if (node == nullptr) { + return 0; + } + int left_depth = MeasureDepth(node->left); + int right_depth = MeasureDepth(node->right); + + return max(left_depth, right_depth) + 1; + } + + int maxDepth(TreeNode* root) { + int depth = MeasureDepth(root); + return depth; + } +}; diff --git a/104.MaximumDepthofBinaryTree/step2.cpp b/104.MaximumDepthofBinaryTree/step2.cpp new file mode 100644 index 0000000..5236888 --- /dev/null +++ b/104.MaximumDepthofBinaryTree/step2.cpp @@ -0,0 +1,31 @@ +/** + * 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 MeasureDepth(TreeNode* node) { + if (node == nullptr) { + return 0; + } + int left_depth = MeasureDepth(node->left); + int right_depth = MeasureDepth(node->right); + + return max(left_depth, right_depth) + 1; + } + + int maxDepth(TreeNode* root) { + if (root == nullptr) { + return 0; + } + int depth = MeasureDepth(root); + return depth; + } +}; diff --git a/104.MaximumDepthofBinaryTree/step3.cpp b/104.MaximumDepthofBinaryTree/step3.cpp new file mode 100644 index 0000000..5236888 --- /dev/null +++ b/104.MaximumDepthofBinaryTree/step3.cpp @@ -0,0 +1,31 @@ +/** + * 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 MeasureDepth(TreeNode* node) { + if (node == nullptr) { + return 0; + } + int left_depth = MeasureDepth(node->left); + int right_depth = MeasureDepth(node->right); + + return max(left_depth, right_depth) + 1; + } + + int maxDepth(TreeNode* root) { + if (root == nullptr) { + return 0; + } + int depth = MeasureDepth(root); + return depth; + } +}; From 94e08f19a2e6d4bd2f7eb999a14d48732ebfa32e Mon Sep 17 00:00:00 2001 From: Ryotaro Kurita Date: Sun, 28 Jul 2024 21:26:58 +0900 Subject: [PATCH 2/3] modified refrecting review --- 104.MaximumDepthofBinaryTree/bfs_step2.cpp | 40 ++++++++++++++++++++++ 104.MaximumDepthofBinaryTree/step4.cpp | 23 +++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 104.MaximumDepthofBinaryTree/bfs_step2.cpp create mode 100644 104.MaximumDepthofBinaryTree/step4.cpp diff --git a/104.MaximumDepthofBinaryTree/bfs_step2.cpp b/104.MaximumDepthofBinaryTree/bfs_step2.cpp new file mode 100644 index 0000000..5d1d049 --- /dev/null +++ b/104.MaximumDepthofBinaryTree/bfs_step2.cpp @@ -0,0 +1,40 @@ +/** + * 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) {} + * }; + */ +struct NodeAndDepth { + TreeNode* node; + int depth; +}; + +class Solution { +public: + int maxDepth(TreeNode* root) { + if (root == nullptr) { + return 0; + } + + queue candidates; + candidates.push({root, 1}); + int max_depth = 0; + while (!candidates.empty()) { + const auto [node, depth] = candidates.front(); + candidates.pop(); + if (node == nullptr) { + continue; + } + max_depth = depth; + candidates.push({node->left, depth + 1}); + candidates.push({node->right, depth + 1}); + } + + return max_depth; + } +}; diff --git a/104.MaximumDepthofBinaryTree/step4.cpp b/104.MaximumDepthofBinaryTree/step4.cpp new file mode 100644 index 0000000..a0b2046 --- /dev/null +++ b/104.MaximumDepthofBinaryTree/step4.cpp @@ -0,0 +1,23 @@ +/** + * 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 == nullptr) { + return 0; + } + int left_depth = maxDepth(root->left); + int right_depth = maxDepth(root->right); + + return max(left_depth, right_depth) + 1; + } +}; From 420c5eb273be15db0b0aa5453c79473b1c95caf8 Mon Sep 17 00:00:00 2001 From: Ryotaro Kurita Date: Wed, 4 Jun 2025 21:09:06 +0900 Subject: [PATCH 3/3] add dfs and bfs solution --- 104.MaximumDepthofBinaryTree/bfs_step3.cpp | 42 ++++++++++++++++++++++ 104.MaximumDepthofBinaryTree/step5.cpp | 28 +++++++++++++++ 104.MaximumDepthofBinaryTree/step6.cpp | 20 +++++++++++ problem_name/step1.cpp | 20 +++++++++++ 4 files changed, 110 insertions(+) create mode 100644 104.MaximumDepthofBinaryTree/bfs_step3.cpp create mode 100644 104.MaximumDepthofBinaryTree/step5.cpp create mode 100644 104.MaximumDepthofBinaryTree/step6.cpp diff --git a/104.MaximumDepthofBinaryTree/bfs_step3.cpp b/104.MaximumDepthofBinaryTree/bfs_step3.cpp new file mode 100644 index 0000000..2b62776 --- /dev/null +++ b/104.MaximumDepthofBinaryTree/bfs_step3.cpp @@ -0,0 +1,42 @@ +/** + * 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; + } + + queue node_and_depth; + node_and_depth.push({root, 1}); + int max_dempth = 0; + while (!node_and_depth.empty()) { + auto [node, depth] = node_and_depth.front(); + node_and_depth.pop(); + + max_dempth = max(max_dempth, depth); + if (node->left) { + node_and_depth.push({node->left, depth + 1}); + } + if (node->right) { + node_and_depth.push({node->right, depth + 1}); + } + } + return max_dempth; + } + +private: + struct NodeAndDepth { + TreeNode* node; + int depth; + }; +}; diff --git a/104.MaximumDepthofBinaryTree/step5.cpp b/104.MaximumDepthofBinaryTree/step5.cpp new file mode 100644 index 0000000..a796cfc --- /dev/null +++ b/104.MaximumDepthofBinaryTree/step5.cpp @@ -0,0 +1,28 @@ +/** + * 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 FindMaxDepth(root); + } + +private: + int FindMaxDepth(TreeNode* node) { + if (!node) { + return 0; + } + return max(FindMaxDepth(node->left), FindMaxDepth(node->right)) + 1; + } +}; diff --git a/104.MaximumDepthofBinaryTree/step6.cpp b/104.MaximumDepthofBinaryTree/step6.cpp new file mode 100644 index 0000000..67da77e --- /dev/null +++ b/104.MaximumDepthofBinaryTree/step6.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* node) { + if (!node) { + return 0; + } + return max(maxDepth(node->left), maxDepth(node->right)) + 1; + } +}; diff --git a/problem_name/step1.cpp b/problem_name/step1.cpp index e69de29..67da77e 100644 --- a/problem_name/step1.cpp +++ b/problem_name/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* node) { + if (!node) { + return 0; + } + return max(maxDepth(node->left), maxDepth(node->right)) + 1; + } +};