原理条件在c++注意点专栏里 可以去看
#include <stdio.h>
#include <malloc.h>
typedef struct lk
{
int data;
struct lk *lchild;
struct lk *rchild;
}bittree;
typedef struct kl
{
int head;
int tail;
int size;
bittree **data;
}queue;
void init(queue *queue,int size)
{
queue->head = -1;
queue->tail = -1;
queue->size = size;
queue->data = (bittree**)malloc(size *sizeof(bittree*));
for(int i = 0 ; i < size ; i++)
{
queue->data[i] = NULL;
}
}
//判满
int isfull(queue *queue)
{
return (queue->tail + 1)%queue->size == queue->head;
}
//判空
int isempty(queue *queue)
{
return queue->head == queue->tail;
}
//入队
void add(queue *queue,bittree *root)
{
queue->tail = (queue->tail + 1 ) % queue->size;
queue->data[queue->tail] = root;
}
//出队
bittree *out(queue *queue)
{
if(isempty(queue)) return NULL;
queue->head = (queue->head + 1 ) % queue->size;
return queue->data[queue->head];
}
bittree *create()
{
bittree *root = NULL;
int num;
scanf("%d",&num);
if(num>0)
{
root = (bittree*)malloc(sizeof(bittree));
root->data = num;
root->lchild = create();
root->rchild = create();
}
}
//是 返回1 不是 返回55
int iser(bittree *root)
{
if(root == NULL) return 1;
int count = 1;
queue queue;
init(&queue,50);
add(&queue,root);
while(!isempty(&queue))
{
root = out(&queue);
// printf("%5d",root->data);
if(root->lchild!=NULL)
{
add(&queue,root->lchild);
// count++;
}
if(root->rchild!=NULL)
{
add(&queue,root->rchild) ;
// count++;
}
if(root->lchild==NULL && root->rchild!=NULL) return 55;
if(root->lchild==NULL && root->rchild==NULL)
{
while(!isempty(&queue))
{
root = out(&queue);
if(root->lchild!=NULL||root->rchild!=NULL) return 55;
}
}
}
if (isempty(&queue)) return 1;
else return 55;
// printf("\n");
// printf("%5d", count);
}
int main()
{
bittree *root = create();
printf("%5d",iser(root));
return 0;
}