Vulkan

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

基本思路:

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

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

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

void LayerTraverse(BinTree BT){
     Queue Q;
     BinTree p=BT;
     if(p!=NULL){
        EnQueue(Q,p);
     }
     while(!IsEmpty(Q)){
         p=DeQueue(Q);
           printf("%c",p->data);
         if(p->lchild!=NULL) 
            EnQueue(Q,p->lchild); 
         if(p->rchild!=NULL){ 
            EnQueue(Q,p->rchild); 
         } 
     } 
 }


posted on 2012-10-16 17:10  Vulkan  阅读(141)  评论(0编辑  收藏  举报

导航