#include<cstdio>
#include<iostream>
#include<stack>
#include<queue>
using namespace std;
#define N 20
#define Null -1
queue<int> q;
struct TreeNode
{
int Left,Right;
}Tree[N];
int BuildTree(struct TreeNode T[]);
void Printf(int F);
int main()
{
int F;
F=BuildTree(Tree);
Printf(F);
return 0;
}
int BuildTree(struct TreeNode T[])
{
int n;
int check[N]={0};
int Root=Null;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
getchar();
char cl,cr;
scanf("%c %c",&cl,&cr);
if(cl!='-')
{
T[i].Left=cl-'0';
check[T[i].Left]=1;
}
else
T[i].Left=Null;
if(cr!='-')
{
T[i].Right=cr-'0';
check[T[i].Right]=1;
}
else
T[i].Right=Null;
}
if(n)
{
for(int i=0;i<n;i++)
{
if(check[i]==0)
{
Root=i;
break;
}
}
}
return Root;
}
void Printf(int F)
{
int flag=0;
int First;
q.push(F);
while(!q.empty())
{
First=q.front();
if(Tree[First].Left==Null&&Tree[First].Right==Null)
{
if(flag==0)
{
printf("%d",First);
flag=1;
}
else
printf(" %d",First);
}
if(Tree[First].Left!=Null)
{
q.push(Tree[First].Left);
}
if(Tree[First].Right!=Null)
{
q.push(Tree[First].Right);
}
q.pop();
}
}
List Leaves
最新推荐文章于 2021-08-23 11:32:33 发布