二叉树按层遍历打印的算法(c/c++)

时间:2021-07-26 11:15:28
void LevelOrder(BTree T, int cnt)
{
    BTree level = malloc(sizeof(struct BTNode)*cnt);
    if(level==NULL) return;
    int i=0,rear=0;
    if(cnt==0) return;
    for(i=0; i<cnt; i++){
        printf("%c ",T[i].data);
        if(T[i].lchild) level[rear++]=*T[i].lchild;
        if(T[i].rchild) level[rear++]=*T[i].rchild;
    }
    printf("\n");
    LevelOrder(level, rear);
    free(level);
} 

main()函数中调用LevelOrder(T,1)