GESP C++ 真题 · 逐题精解
首页C++六级真题 › 2024年3月 › 第8题

GESP 2024年3月 C++六级 单选题 第8题

C++六级单选题2024年3月第8题

所属知识点:广度优先搜索(BFS) 难度要求:— 考频:—

用下面 BFS 代码遍历给定二叉树(1 的孩子 2、3;2 的孩子 8、9;3 的右孩子 6;9 的孩子 4、5;6 的右孩子 7;7 的孩子 10、11),可能的输出是:
void bfs(TreeNode* root) {
    if (!root) return;
    queue<TreeNode*> q; q.push(root);
    while (!q.empty()) {
        TreeNode* cur = q.front(); q.pop();
        cout << cur->val << " ";
        if (cur->left) q.push(cur->left);
        if (cur->right) q.push(cur->right);
    }
}

正确答案:C

题目解析

BFS 按层输出:1 | 2,3 | 8,9,6 | 4,5,7 | 10,………

完整解析为会员内容二级及以上的逐题精讲需开通 VIP。一级解析全部免费。前往 GESPPASS 解锁

想系统刷完 GESP C++ 1~8 级真题,并查看每道题的逐题精讲?

进入 GESPPASS 开始练习