Skip to content

111. Minimum Depth of Binary Tree#21

Open
5103246 wants to merge 1 commit intomainfrom
111-minimum-depth-of-binary-tree
Open

111. Minimum Depth of Binary Tree#21
5103246 wants to merge 1 commit intomainfrom
111-minimum-depth-of-binary-tree

Conversation

@5103246
Copy link
Copy Markdown
Owner

@5103246 5103246 commented 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文を省略しなくてもいい気がする。
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

実行されることがないコードを Dead code といいます。むしろ書かないほうがいいと私は思いますね。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

デッドコードと言うんですね。
デッドコードがあると読みやすさや保守性が低下するため、書かない方がよい、ということでしょうか?

Copy link
Copy Markdown

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

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;
}
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

読みやすいです

Copy link
Copy Markdown

@nanae772 nanae772 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

全体的に読みやすかったです

```

- とりあえずキューに入れて、nullptrだったら飛ばす
- root == nullptrだったら、末行で0が返ってくる
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ぱっと見でちょっと分からなかったので個人的には最初にチェックしてreturnしてしまうほうが分かりやすいなと思いました

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants