#include <cstdio>
#include <queue>
using namespace std;
struct node
{
int data;
node* lc;
node* rc;
};
int n,pos[100],in[100];
node* creat(int pl,int pr,int il,int ir)
{
if(pl > pr)
return NULL;
node* root = new node;
root->data = pos[pr];
int temp = root->data;
int i;
for(i = il;i<=ir;i++)
{
if(in[i] == pos[pr])
{
break;
}
}
int numl = ir - i;
root->lc = creat(pl,pr - numl - 1,il,i-1);
root->rc = creat(pr - numl,pr - 1,i+1,ir);
return root;
}
void proT(node*root)
{
if(root == NULL)
return;
printf("%d ",root->data);
proT(root->lc);
proT(root->rc);
}
void levelT(node*root)
{
int flag = 0;
queue<node*>q;
q.push(root);
while(!q.empty())
{
node* s = q.front();
q.pop();
if(flag == 0)
{
printf("%d",s->data);
flag = 1;
}
else
{
printf(" %d",s->data);
}
if(s->lc != NULL)
{
q.push(s->lc);
}
if(s->rc != NULL)
{
q.push(s->rc);
}
}
}
int main()
{
freopen("in.txt","r",stdin);
scanf("%d",&n);
for(int i = 1;i<=n;i++)
{
scanf("%d",&pos[i]);
}
for(int i = 1;i<=n;i++)
{
scanf("%d",&in[i]);
}
levelT(creat(1,n,1,n));
return 0;
}
9.2 1020 Tree Traversals (25 分)(***)
最新推荐文章于 2022-01-20 16:33:51 发布