判断完全二叉树

int IsComplete(BiTree T)//判断二叉树是否完全二叉树,是则返回1,否则返回0 
{ 
  InitQueue(Q); 
  BiTree p;
  int flag = 0; 
  EnQueue(Q,T);
  while(!IsEmpty(Q)) {  
    DeQueue(Q,p); 
    if(!p)  //结点为空
       flag = 1; 
    else if(flag) //结点非空且flag为1
       return 0; 
    else {  //结点非空且flag为0,将结点左右孩子入队
      EnQueue(Q,p->lchild); 
      EnQueue(Q,p->rchild); //孩子无需判空
    } 
  }
  return 1; 
}
//已经包含空树的情况了

 

posted @ 2022-11-17 20:20  葫芦锤  阅读(19)  评论(0)    收藏  举报  来源