以下函数 check() 用于判断一棵二叉树是否为( )。
bool check(TreeNode* root) {
if (!root) return true;
queue<TreeNode*> q; q.push(root);
bool hasNull = false;
while (!q.empty()) {
TreeNode* cur = q.front(); q.pop();
if (!cur) hasNull = true;
else { if (hasNull) return false; q.push(cur->left); q.push(cur->right); }
}
return true;
}
- A. 满二叉树
- B. 完全二叉树
- C. 二叉搜索树
- D. 平衡二叉树
正确答案:B