判断是否是完全二叉树:找到第一个度不为2的节点标记起来,如果后面的节点还有孩子则不是完全二叉树
bool _IsCompleteBinaryTree(Node* _pRoot)
{
if (_pRoot == NULL)
return true;
Node* pCur = _pRoot;
if ((pCur->_pLeft == NULL) && (pCur->_pRight == NULL))
return true;
queue<Node*> q;
q.push(pCur);
bool flag = false;
size_t count = 0;
while (!q.empty())
{
Node* front = q.front();
if ((front != NULL) && ((front->_pLeft) || (front->_pRight)))
{
q.push(front->_pLeft);
q.push(front->_pRight);
}
q.pop();
if (front == NULL)
{
flag = true;
count++;
}
else
{
if (count == 1)
{
flag = false;
break;
}
else
flag = false;
}
}
if (flag == false)
return false;
else
return true;
}