本文主要是介绍判断一棵二叉树是不是完全二叉树,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
判断是否是完全二叉树:找到第一个度不为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;}elseflag = false;}}if (flag == false)return false;elsereturn true;}
这篇关于判断一棵二叉树是不是完全二叉树的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!