二叉树层次遍历算法+非递归

基本思路:

(1)若树节点非空,则入队。

(2)把对头的左右节点入队(非空),出队(并输出结果)

(3)重复步骤(2)直到对为空

算法:

 1 void LayerTraverse(BinTree BT){
 2     Queue Q;
 3     BinTree p=BT;
 4     if(p!=NULL){
 5        EnQueue(Q,p);
 6     }
 7     while(!IsEmpty(Q)){
 8         p=DeQueue(Q);
printf("%c",p->data);
9 if(p->lchild!=NULL)
10 EnQueue(Q,p->lchild);
11 if(p->rchild!=NULL){
12 EnQueue(Q,p->rchild);
14 }
15     } 
16 }

 

posted on 2012-05-22 22:25  为梦飞翔  阅读(5695)  评论(0编辑  收藏  举报

导航