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

GESP 2025年3月 C++六级 单选题 第4题

C++六级单选题2025年3月第4题

所属知识点:完全二叉树与性质 难度要求:— 考频:—

以下代码的功能是:
bool isCompleteTree(TreeNode* root) {
    if (!root) return true;
    queue<TreeNode*> q; q.push(root);
    bool hasNull = false;
    while (!q.empty()) {
        TreeNode* node = q.front(); q.pop();
        if (node == nullptr) hasNull = true;
        else { if (hasNull) return false; q.push(node->left); q.push(node->right); }
    }
    return true;
}

正确答案:B

题目解析
BFS 层序入队(含空节点),一旦遇到空节点后又出现非空节点则返回 false——这是判断完全二叉树的标准方法,选 B。

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

进入 GESPPASS 开始练习