diff --git a/.DS_Store b/.DS_Store index 4f25a51..3dc170b 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/104.MaximumDepthofBinaryTree/bfs.cpp b/104.MaximumDepthofBinaryTree/bfs.cpp new file mode 100644 index 0000000..18dbffd --- /dev/null +++ b/104.MaximumDepthofBinaryTree/bfs.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 = max(depth, max_depth); + candidates.push({node->left, depth + 1}); + candidates.push({node->right, depth + 1}); + } + + return max_depth; + } +}; 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/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/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; + } +}; 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; + } +}; 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; + } +};