#include <bits/stdc++.h>
using namespace std;
struct node
{
char data;
node *left, *right;
};
int i;
char str[60];
node *creat();
void levelorder(node *root);
int main()
{
int n;
cin >> n;
while(n --)
{
node *root;
scanf("%s", str);
i = 0;
root = creat();
levelorder(root);
cout << endl;
}
return 0;
}
node *creat()
{
node *tail;
if(str[i] == ',')
{
tail = NULL;
i++;
}
else
{
tail = new node;
tail -> data = str[i];
i ++;
tail -> left = creat();
tail -> right = creat();
}
return tail;
}
void levelorder(node *root)
{
queue<node *>Q;
Q.push(root);
node *tmp = new node;
while(!Q.empty())
{
tmp = Q.front();
Q.pop();
if(tmp)
{
cout << tmp -> data;
Q.push(tmp -> left);
Q.push(tmp -> right);
}
}
}
SDUT 3344 数据结构实验之二叉树五:层序遍历
最新推荐文章于 2021-01-28 18:51:19 发布