层次遍历建树

bitree *creatbintree(){//层次建树
    bitree *T;
    int c;
    queue q;
    scanf("%d",&c);
    if(c==0){
        return NULL;
    }
    T=(bitree*)malloc(sizeof(bitree));
    T->data=c;
    q.push(T);
    while(q.empty()==false){
        bitree *T=q.front();
        q.pop();
        scanf("%d",&c);
        if(c==0){
            T->lchild=NULL;
        }
        else{
            bitree *p=(bitree*)malloc(sizeof(bitree));
            p->data=c;
            T->lchild=p;
            q.push(p);
        }
        scanf("%d",&c);
        if(c==0){
            T->rchild=NULL;
        }
        else{
            bitree *p=(bitree*)malloc(sizeof(bitree));
            p->data=c;
            T->rchild=p;
            q.push(p);
        }
    }
    return T;
}

你可能感兴趣的:(层次遍历建树)