# 层次遍历二叉树

#include<stdio.h>
#include<stdlib.h>
int rear=0,front=0;
typedef struct node {
int data;
struct node* lchild,*rchild;
} BiTNode,*BiTree;
void initTree(BiTree* t) {
*t=(BiTNode*)malloc(sizeof(BiTNode));
(*t)->data=1;

(*t)->lchild=(BiTNode*)malloc(sizeof(BiTNode));
(*t)->lchild->data=2;

(*t)->rchild=(BiTNode*)malloc(sizeof(BiTNode));
(*t)->rchild->data=3;

(*t)->lchild->lchild=(BiTNode*)malloc(sizeof(BiTNode));
(*t)->lchild->lchild->data=4;
(*t)->lchild->lchild->lchild=NULL;
(*t)->lchild->lchild->rchild=NULL;

(*t)->lchild->rchild=(BiTNode*)malloc(sizeof(BiTNode));
(*t)->lchild->rchild->data=5;
(*t)->lchild->rchild->lchild=NULL;
(*t)->lchild->rchild->rchild=NULL;

(*t)->rchild->lchild=(BiTNode*)malloc(sizeof(BiTNode));
(*t)->rchild->lchild->data=6;
(*t)->rchild->lchild->lchild=NULL;
(*t)->rchild->lchild->rchild=NULL;

(*t)->rchild->rchild=(BiTNode*)malloc(sizeof(BiTNode));
(*t)->rchild->rchild->data=7;
(*t)->rchild->rchild->lchild=NULL;
(*t)->rchild->rchild->rchild=NULL;
}
void displayElem(BiTNode* a){
printf("%d ",a->data);
}
void enQueue(BiTree* d,BiTree n){
d[rear++]=n;
}
BiTNode* deQueue(BiTree* d){
return d[front++];
}
int main(){
BiTree t;
initTree(&t);
BiTNode* q;
BiTNode* a[10];
enQueue(a,t);
while(front<rear){
q=deQueue(a);
displayElem(q);
if(q->lchild)
enQueue(a,q->lchild);
if(q->rchild)
enQueue(a,q->rchild);
}
}


05-11 1万+

06-16 1万+

05-21 5

12-03 3294

04-09 6万+

08-01 5084

08-11 255

10-25 585

08-29 477

12-27 673

02-19 4205

06-16 4413

04-06 1959

07-22 1857

03-01 2105

07-20 2670

07-04 40

04-25 2267

09-15 2341

#### 层次遍历二叉树从上到下左往右遍历

©️2019 CSDN 皮肤主题: 游动-白 设计师: 上身试试