ustc1


私信TA

用户名:ustc1

访问量:1303

签 名:

等  级
排  名 3729
经  验 1778
参赛次数 4
文章发表 2
年  龄 0
在职情况 学生
学  校 科大
专  业

  自我简介:

解题思路:

注意事项:

参考代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
typedef struct tree
{
    int m;
    struct tree *l;
    struct tree *r;
}T;
void build(T *root,T *s)
{
    if(s->m<root->m)
    {
        if(root->l)
        {
            build(root->l,s);
        }
        else
        {
            root->l=s;
        }
    }
    else if(s->m>root->m)
    {
        if(root->r)
        {
            build(root->r,s);
        }
        else
        {
            root->r=s;
        }
    }
}
void xian(T *s)
{
    if(s)
    {
        printf("%d ",s->m);
        xian(s->l);
        xian(s->r);
    }
}
void zhong(T *s)
{
     if(s)
    {
        zhong(s->l);
        printf("%d ",s->m);
        zhong(s->r);
    }
}
void hou(T *s)
{
     if(s)
    {
        hou(s->l);
        hou(s->r);
        printf("%d ",s->m);
    }
}
int main()
{
    int N;
    int a[111];
    while((scanf("%d",&N))!=EOF)
    {
       memset(a,0,sizeof(a));
       int i;
       for(i=0;i<N;i++)
       {
           scanf("%d",&a[i]);
       }
       T *root;
       root=malloc(sizeof(T));
       root->m=a[0];
       root->l=NULL;
       root->r=NULL;
       for(i=1;i<N;i++)
       {
           T *s;
           s=malloc(sizeof(T));
           s->m=a[i];
           s->l=NULL;
           s->r=NULL;
           build(root,s);
       }
       xian(root);
       printf("\n");
       zhong(root);
       printf("\n");
       hou(root);
       printf("\n");
    }
    return 0;
}

 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区