#include <iostream>
using namespace std;
struct BitNode
{
char data;
BitNode *lchild;
BitNode *rchild;
};
struct LinkNode
{
BitNode *data;
LinkNode *next;
};
struct LinkQueue
{
LinkNode *front;
LinkNode *rear;
};
void init(LinkQueue *&Q)
{
Q=new LinkQueue;
Q->front=new LinkNode;
Q->front->next=NULL;
Q->rear=Q->front;
}
bool isEmpty(LinkQueue *Q)
{
return Q->rear==Q->front;
}
void push(LinkQueue *&Q,BitNode *e)
{
LinkNode *p=new LinkNode;
p->data=e;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
}
void pop(LinkQueue *&Q)
{
if(!isEmpty(Q))
{
LinkNode *p=Q->front->next;
Q->front->next=p->next;
if(Q->rear==p)
Q->rear=Q->front;
delete p;
}
}
BitNode *get(LinkQueue *Q)
{
if(!isEmpty(Q))
return Q->front->next->data;
}
void create(BitNode *&T)
{
char ch;
cin>>ch;
if(ch=='#')
T=NULL;
else
{
T=new BitNode;
T->data=ch;
create(T->lchild);
create(T->rchild);
}
}
void proOrder(BitNode *T)
{
if(T)
{
cout<<T->data<<" ";
proOrder(T->lchild);
proOrder(T->rchild);
}
}
bool isCompleteTree(BitNode *T)
{
LinkQueue *Q;
init(Q);
push(Q,T);
while(1)
{
BitNode *t=get(Q);
if(t==NULL)
break;
pop(Q);
push(Q,t->lchild);
push(Q,t->rchild);
}
while(!isEmpty(Q))
{
BitNode *t=get(Q);
if(t!=NULL)
return false;
pop(Q);
}
return true;
}
int main()
{
BitNode *T;
create(T);
return 0;
}
using namespace std;
struct BitNode
{
char data;
BitNode *lchild;
BitNode *rchild;
};
struct LinkNode
{
BitNode *data;
LinkNode *next;
};
struct LinkQueue
{
LinkNode *front;
LinkNode *rear;
};
void init(LinkQueue *&Q)
{
Q=new LinkQueue;
Q->front=new LinkNode;
Q->front->next=NULL;
Q->rear=Q->front;
}
bool isEmpty(LinkQueue *Q)
{
return Q->rear==Q->front;
}
void push(LinkQueue *&Q,BitNode *e)
{
LinkNode *p=new LinkNode;
p->data=e;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
}
void pop(LinkQueue *&Q)
{
if(!isEmpty(Q))
{
LinkNode *p=Q->front->next;
Q->front->next=p->next;
if(Q->rear==p)
Q->rear=Q->front;
delete p;
}
}
BitNode *get(LinkQueue *Q)
{
if(!isEmpty(Q))
return Q->front->next->data;
}
void create(BitNode *&T)
{
char ch;
cin>>ch;
if(ch=='#')
T=NULL;
else
{
T=new BitNode;
T->data=ch;
create(T->lchild);
create(T->rchild);
}
}
void proOrder(BitNode *T)
{
if(T)
{
cout<<T->data<<" ";
proOrder(T->lchild);
proOrder(T->rchild);
}
}
bool isCompleteTree(BitNode *T)
{
LinkQueue *Q;
init(Q);
push(Q,T);
while(1)
{
BitNode *t=get(Q);
if(t==NULL)
break;
pop(Q);
push(Q,t->lchild);
push(Q,t->rchild);
}
while(!isEmpty(Q))
{
BitNode *t=get(Q);
if(t!=NULL)
return false;
pop(Q);
}
return true;
}
int main()
{
BitNode *T;
create(T);
if(isCompleteTree(T))
cout<<"ok"<<endl;
else
cout<<"no"<<endl;
return 0;
}