二叉树的建立(先序转换为层序)

问题 D: 二叉树的建立

描述
给定一个序列,按先序序列建立二叉树。输出建立后的二叉树的从上到下层次遍历序列。
格式
输入格式
一个序列
输出格式
从上之下层次序列
样例
样例输入
ABC##DE#G##F###
样例输出
ABCDEFG

从这个题入手真的好。

#include<stdio.h>
struct node
{
    struct node *l;
    struct node *r;
    char data;
};
int _creat()
{
    struct node *t;
    char x;
    scanf("%c",&x);
    if(x=='#')
        t=NULL;
    else{
        t=(struct node *)malloc(sizeof(struct node));
        t->data=x;
        t->l=_creat();
        t->r=_creat();
    }
    return(t);
}
void Printf(struct node *head)
{
    struct node *a[10100];
    int h=0,t=0;
    if(head)
    {
        a[t++]=head;
        while(h!=t)
        {
            printf("%c",a[h]->data);
            if(a[h]->l)
                a[t++]=a[h]->l;
            if(a[h]->r)
                a[t++]=a[h]->r;
            h++;
        }
    }
}
int main()
{
    struct node *head=_creat();
    Printf(head);
    return(0);
}

发布了8 篇原创文章 · 获赞 3 · 访问量 493

猜你喜欢

转载自blog.csdn.net/m0_46193982/article/details/104089549