Open
Conversation
nanae772
reviewed
Oct 7, 2025
| while (!node_and_depth.empty()) { | ||
| auto [node, depth] = node_and_depth.front(); | ||
| node_and_depth.pop(); | ||
| max_depth = max(max_depth, depth); |
There was a problem hiding this comment.
BFSだと深さが減ることは無いので以下のように書いてもよさそうです。
Suggested change
| max_depth = max(max_depth, depth); | |
| max_depth = depth; |
| return 0; | ||
| } | ||
| int max_depth = 1; | ||
| stack<TreeNode*> tree_path; |
| - すべてのノードを調べ終えたら、スタックを空にするまでpopしてループなどの処理をするのが無駄に感じたが、せいぜい計算量が2倍になる程度なので別にいいか。 | ||
|
|
||
| ```cpp | ||
| class Solution { |
| return 0; | ||
| } | ||
| int max_depth = 1; | ||
| queue<pair<TreeNode*, int>> node_and_depth; |
There was a problem hiding this comment.
複数形であることを示すためにnode_depth_pairsなどにしてもよさそうかなと思いました
| while (!tree_path.empty()) { | ||
| TreeNode* node = tree_path.top(); | ||
|
|
||
| while (node->left || node->right) { |
There was a problem hiding this comment.
どの道48行目でループを抜けられるのでwhile (true)でもよさそうですかね?
akmhmgc
reviewed
Oct 9, 2025
| return 0; | ||
| } | ||
| queue<pair<TreeNode*, int>> node_and_depth; | ||
| node_and_depth.emplace(root, 1); |
There was a problem hiding this comment.
BFSであれば階層が変わるごとにdeqthを足せば良いので、depthを持たせずに書くこともできそうですね。
以下のようなイメージです。
class Solution {
public:
int maxDepth(TreeNode* root) {
if (!root) {
return 0;
}
std::queue<TreeNode*> nodes;
nodes.push(root);
int max_depth = 0;
while (!nodes.empty()) {
int level_size = nodes.size();
max_depth++;
for (int i = 0; i < level_size; ++i) {
TreeNode* node = nodes.front();
nodes.pop();
if (node->left) {
nodes.push(node->left);
}
if (node->right) {
nodes.push(node->right);
}
}
}
return max_depth;
}
};
Owner
Author
There was a problem hiding this comment.
コードありがとうございます!
こちらのコードもわかりやすくていいですね。
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/maximum-depth-of-binary-tree/
次に解く問題
https://leetcode.com/problems/minimum-depth-of-binary-tree/