完全二叉树的判定

从根节点开始将每个节点的左右孩子压入队列(如果为空也压入);遇到第一个NULL时结束再判断是否队列为空且第一个节点是否为NULL

【代码】

bool judge(tree &T)
{
	if(T==NULL)return true;
	queue<tree>q;
	q.push(T);
	tree t;
	while(t=q.front())
	{
		q.push(t->lchild);
		q.push(t->rchild);
		q.pop();
	}
	while(!q.empty())
	{
		if(q.front()!=NULL)return false;
		q.pop();
	}
	return true;
}

猜你喜欢

转载自blog.csdn.net/qq_38735931/article/details/88065313