#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
using namespace std;
char a[100];
int k;
struct node
{
int data;
struct node *lchild,*rchild;
};
struct node *creat()
{
struct node*root;
char c;
c=a[k++];
if(c==',')
return NULL;
else
{
root = (struct node*)malloc(sizeof(struct node));
root->data=c;
root->lchild=creat();
root->rchild=creat();
}
return root;
};
int sequence(struct node *root)//栈形式
{
queue<struct node *>s;
s.push(root);
while(!s.empty())
{
root=s.front();
s.pop();
if(root)
{
cout<<root->data;
s.push(root->lchild);
s.push(root->rchild);
}
}
}
/*
int sequence(struct node *root)//数组形式
{
int pop=0,push=0;
struct node *q[100];
q[push++]=root;
while(push>pop)
{
if(q[pop])
{
printf("%c",q[pop]->data);
q[push++]=q[pop]->lchild;
q[push++]=q[pop]->rchild;
}
pop++;
}
}
*/
int main()
{
int t;
ios::sync_with_stdio(false);//cin.tie(0);
struct node *root;
cin>>t;
while(t--)
{
k=0;
cin>>a;
root=(struct node *)malloc(sizeof(struct node));
root=creat();sequence(root);
printf("\n");
}
return 0;
}
3344 数据结构实验之二叉树五:层序遍历
最新推荐文章于 2018-11-08 15:15:59 发布