-
Notifications
You must be signed in to change notification settings - Fork 0
104. Maximum Depth of Binary Tree #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<NodeAndDepth> 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; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<NodeAndDepth> 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; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<NodeAndDepth> 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; | ||
| }; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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など | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
|
Comment on lines
+25
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. root == nullptrを特別扱いする必要がないように見えます。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @fhiyo |
||
| int depth = MeasureDepth(root); | ||
| return depth; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. この関数を公開する必要がないように見えました。privateでいいのかなと思います。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. メンバー変数使わないのと、引数のノードも変更しないので、以下のように書けます。 また、maxDepthとMeasureDepthは、呼び出し側からすると完全に同じ振る舞いをします。一つにまとめて良さそうです。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @fhiyo @liquo-rice |
||
| 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; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. この関数、単に return MeasureDepth(root); でよくないでしょうか。としたら、二つを一つにしてもいいのではないでしょうか。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leetcode の都合上変えられないので仕方ないですが、引数に渡される TreeNode がルートノードなのは最初だけなので、これを root とするのは適当でないように思いました。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 各関数呼び出しを考えると、そのnodeを根とするツリーの その場合、 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
あーなるほどその発想はありませんでした。そう考えると確かに不自然ではないかもしれないです。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Java ならまあありそうだなって感じの長さに思いました。 C++ だとどうなんでしょうね |
||
| if (root == nullptr) { | ||
| return 0; | ||
| } | ||
| int left_depth = maxDepth(root->left); | ||
| int right_depth = maxDepth(root->right); | ||
|
|
||
| return max(left_depth, right_depth) + 1; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
必ず、depth >= max_depthではないですか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
私もここは常にdepthのほうが大きいと思います.
でも同じことになると思います.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@liquo-rice @fhiyo
常に更新されているので意味無かったですね。修正しました。