Open
Conversation
oda
reviewed
Oct 8, 2025
| - とりあえずpush_backして、nullptrだったら飛ばすのでもいいのか。 | ||
| - 無限ループにしたら、末行でreturnしなくて済むのか。 | ||
| - https://stackoverflow.com/questions/58520107/do-i-need-a-return-statement-after-an-infinite-loop?utm_source=chatgpt.com | ||
| - 問題はないそうだが、コンパイラによっては警告を出すらしい。return文を省略しなくてもいい気がする。 |
There was a problem hiding this comment.
実行されることがないコードを Dead code といいます。むしろ書かないほうがいいと私は思いますね。
Owner
Author
There was a problem hiding this comment.
デッドコードと言うんですね。
デッドコードがあると読みやすさや保守性が低下するため、書かない方がよい、ということでしょうか?
There was a problem hiding this comment.
そういうことです。
誤った引数によっては到達する場合 never reach などと書くこともありますが。
https://source.chromium.org/search?q=%22never%20reach%22
akmhmgc
reviewed
Oct 9, 2025
Comment on lines
+152
to
+170
| 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; | ||
| } | ||
| }; |
nanae772
reviewed
Oct 9, 2025
| ``` | ||
|
|
||
| - とりあえずキューに入れて、nullptrだったら飛ばす | ||
| - root == nullptrだったら、末行で0が返ってくる |
There was a problem hiding this comment.
ぱっと見でちょっと分からなかったので個人的には最初にチェックしてreturnしてしまうほうが分かりやすいなと思いました
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
問題文
https://leetcode.com/problems/minimum-depth-of-binary-tree/
次に解く問題
https://leetcode.com/problems/merge-two-binary-trees/