-
Notifications
You must be signed in to change notification settings - Fork 0
111. Minimum Depth of Binary Tree #21
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
Open
5103246
wants to merge
1
commit into
main
Choose a base branch
from
111-minimum-depth-of-binary-tree
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
171 changes: 171 additions & 0 deletions
171
111-minimum-depth-of-binary-tree/111-minimum-depth-of-binary-tree.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| # Minimum Depth of Binary Tree | ||
| * 問題: https://leetcode.com/problems/minimum-depth-of-binary-tree/ | ||
| * 言語: C++ | ||
|
|
||
| ## Step1 | ||
|
|
||
| - 前回と同じように、DFSとBFSの両方で解いてみた。 | ||
| - DFSは左ノードや右ノードがない場合のケースも考慮する必要がある。 | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| int minDepth(TreeNode* root) { | ||
| if (!root) { | ||
| return 0; | ||
| } | ||
| if (!root->left) { | ||
| return minDepth(root->right) + 1; | ||
| } | ||
| if (!root->right) { | ||
| return minDepth(root->left) + 1; | ||
| } | ||
|
|
||
| return min(minDepth(root->left), minDepth(root->right)) + 1; | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| - BFSはqueueでノードと深さを管理する。葉ノードに達したときの深さが最小の深さなのでreturn | ||
| - ノードと深さを別にする解き方もあるが、ループが増えるので一緒にqueueで管理した方が楽だと思ったので、一緒にして実装。 | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| int minDepth(TreeNode* root) { | ||
| if (!root) { | ||
| return 0; | ||
| } | ||
| queue<pair<TreeNode*, int>> node_and_depth; | ||
| node_and_depth.emplace(root, 1); | ||
| while (!node_and_depth.empty()) { | ||
| auto [node, depth] = node_and_depth.front(); | ||
| node_and_depth.pop(); | ||
| if (!node->left && !node->right) { | ||
| return depth; | ||
| } | ||
| if (node->left) { | ||
| node_and_depth.emplace(node->left, depth + 1); | ||
| } | ||
| if (node->right) { | ||
| node_and_depth.emplace(node->right, depth + 1); | ||
| } | ||
| } | ||
| return 0; | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| ## Step2 | ||
|
|
||
| - https://github.com/irohafternoon/LeetCode/pull/24/files | ||
| - https://github.com/nktr-cp/leetcode/pull/23/files | ||
| - 深さごとに管理する方法で解いてみた。 | ||
| - https://github.com/irohafternoon/LeetCode/pull/24/files#r2052736369 | ||
| - とりあえずpush_backして、nullptrだったら飛ばすのでもいいのか。 | ||
| - 無限ループにしたら、末行でreturnしなくて済むのか。 | ||
| - https://stackoverflow.com/questions/58520107/do-i-need-a-return-statement-after-an-infinite-loop?utm_source=chatgpt.com | ||
| - 問題はないそうだが、コンパイラによっては警告を出すらしい。return文を省略しなくてもいい気がする。 | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| int minDepth(TreeNode* root) { | ||
| if (!root) { | ||
| return 0; | ||
| } | ||
| vector<TreeNode*> current_nodes{root}; | ||
| int depth = 1; | ||
| while (true) { | ||
| vector<TreeNode*> next_nodes; | ||
| for (auto node : current_nodes) { | ||
| if (!node->left && !node->right) { | ||
| return depth; | ||
| } | ||
| if (node->left) { | ||
| next_nodes.push_back(node->left); | ||
| } | ||
| if (node->right) { | ||
| next_nodes.push_back(node->right); | ||
| } | ||
| } | ||
| current_nodes.swap(next_nodes); | ||
| ++depth; | ||
| } | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| - とりあえずキューに入れて、nullptrだったら飛ばす | ||
| - root == nullptrだったら、末行で0が返ってくる | ||
|
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してしまうほうが分かりやすいなと思いました |
||
| - こっちの方がすっきりしていて好み。 | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| int minDepth(TreeNode* root) { | ||
| queue<pair<TreeNode*, int>> node_and_depth; | ||
| node_and_depth.emplace(root, 1); | ||
| while (!node_and_depth.empty()) { | ||
| auto [node, depth] = node_and_depth.front(); | ||
| node_and_depth.pop(); | ||
| if (!node) { | ||
| continue; | ||
| } | ||
| if (!node->left && !node->right) { | ||
| return depth; | ||
| } | ||
| node_and_depth.emplace(node->left, depth + 1); | ||
| node_and_depth.emplace(node->right, depth + 1); | ||
| } | ||
| return 0; | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| ## Step3 | ||
|
|
||
| - DFS | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| int minDepth(TreeNode* root) { | ||
| if (!root) { | ||
| return 0; | ||
| } | ||
| if (!root->left) { | ||
| return minDepth(root->right) + 1; | ||
| } | ||
| if (!root->right) { | ||
| return minDepth(root->left) + 1; | ||
| } | ||
| return min(minDepth(root->left), minDepth(root->right)) + 1; | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| - BFS | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| int minDepth(TreeNode* root) { | ||
| queue<pair<TreeNode*, int>> node_and_depth; | ||
| node_and_depth.emplace(root, 1); | ||
| while (!node_and_depth.empty()) { | ||
| auto [node, depth] = node_and_depth.front(); | ||
| node_and_depth.pop(); | ||
| if (!node) { | ||
| continue; | ||
| } | ||
| if (!node->left && !node->right) { | ||
| return depth; | ||
| } | ||
| node_and_depth.emplace(node->left, depth + 1); | ||
| node_and_depth.emplace(node->right, depth + 1); | ||
| } | ||
| return 0; | ||
| } | ||
| }; | ||
|
Comment on lines
+152
to
+170
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. 読みやすいです |
||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
実行されることがないコードを Dead code といいます。むしろ書かないほうがいいと私は思いますね。
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.
デッドコードと言うんですね。
デッドコードがあると読みやすさや保守性が低下するため、書かない方がよい、ということでしょうか?
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.
そういうことです。
誤った引数によっては到達する場合 never reach などと書くこともありますが。
https://source.chromium.org/search?q=%22never%20reach%22