Skip to content

104. Maximum Depth of Binary Tree#20

Open
5103246 wants to merge 1 commit intomainfrom
104-maximum-depth-of-binary-tree
Open

104. Maximum Depth of Binary Tree#20
5103246 wants to merge 1 commit intomainfrom
104-maximum-depth-of-binary-tree

Conversation

@5103246
Copy link
Copy Markdown
Owner

@5103246 5103246 commented Oct 7, 2025

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.

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

while (!node_and_depth.empty()) {
auto [node, depth] = node_and_depth.front();
node_and_depth.pop();
max_depth = max(max_depth, depth);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

BFSだと深さが減ることは無いので以下のように書いてもよさそうです。

Suggested change
max_depth = max(max_depth, depth);
max_depth = depth;

return 0;
}
int max_depth = 1;
stack<TreeNode*> tree_path;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

分かりやすい変数名で良いなと思いました

- すべてのノードを調べ終えたら、スタックを空にするまでpopしてループなどの処理をするのが無駄に感じたが、せいぜい計算量が2倍になる程度なので別にいいか。

```cpp
class Solution {
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 0;
}
int max_depth = 1;
queue<pair<TreeNode*, int>> node_and_depth;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

複数形であることを示すためにnode_depth_pairsなどにしてもよさそうかなと思いました

while (!tree_path.empty()) {
TreeNode* node = tree_path.top();

while (node->left || node->right) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

どの道48行目でループを抜けられるのでwhile (true)でもよさそうですかね?

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.

そうですね。

return 0;
}
queue<pair<TreeNode*, int>> node_and_depth;
node_and_depth.emplace(root, 1);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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;
    }
};

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.

コードありがとうございます!
こちらのコードもわかりやすくていいですね。

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.

3 participants