第23833题 单选题
基于广度优先搜索的C++二叉树深度计算代码横线处应填入的正确内容是?

上一题的二叉树深度计算还可以采用二叉树的广度优先搜索来实现。以下基于二叉树的广度优先搜索实现的深度计算函数中横线上应填写( )。

#include <queue>

int max_depth_bfs(tree_node* root) {
    if (root == nullptr) {
        return 0; // 如果树为空,深度为 0
    }

    queue <tree_node*> q;
    q.push(root);
    int depth = 0;

    // 使用队列进行层序遍历
    while (!q.empty()) {
        ________________ // 在此处填入代码
        for (int i = 0; i < level_size; ++i) {
            tree_node* node = q.front();
            q.pop();

            if (node->left) {
                q.push(node->left);
            }
            if (node->right) {
                q.push(node->right);
            }
        }
    }

    return depth;
}
{{ option.label }}
子题{{ index + 1 }} {{ child.type_label }}
{{ option.label }}
✓ 正确 ◐ 部分正确 ✗ 错误
程序运行统计
暂无判题统计
提交{{ questionInfo.stats ? questionInfo.stats.submit_count : 0 }}次 正确率{{ statsAccuracy }}%
答案解析