数据结构实验之二叉树五:层序遍历 (SDUT 3344)

2023-03-09 17:09:36 浏览数 (1)

代码语言:javascript复制
#include <bits/stdc  .h>
using namespace std;
struct node
{
    char data;
    struct node *lc, *rc;
};
char s[505];
int num;
struct node *creat()
{
    struct node *root;
    if(s[num   ] == ',')
    {
        root = NULL;
    }
    else
    {
        root = new node;
        root -> data = s[num - 1];
        root -> lc = creat();
        root -> rc = creat();
    }
    return root;
};
void level(struct node *root)
{
       struct node *q[100];
       int in = 0, out = 0;
       q[in  ] = root;
       while(out < in)
       {
           if(q[out])
           {
               printf("%c", q[out] -> data);
               q[in  ] = q[out] -> lc;
               q[in  ] = q[out] -> rc;
           }
           out  ;

       }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",s);
        num = 0;
        struct node *root;
        root = creat();
        level(root);
        printf("n");
    }
    return 0;
}

0 人点赞