Description
已知一个按先序输入的字符序列,如abd,,eg,,,cf,,,(其中,表示空结点)。请建立该二叉树并按从上到下从左到右的顺序输出该二叉树的所有叶子结点。
Input
输入数据有多行,每一行是一个长度小于50个字符的字符串。
Output
按从上到下从左到右的顺序输出二叉树的叶子结点。
Sample
Input
abd,,eg,,,cf,,, xnl,,i,,u,,
Output
dfg uli
Hint
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef struct node
{
int data;
struct node *left;
struct node *right;
} tree;
char str[55];
int flag=0;
tree*creat(tree*root)
{
if(str[flag]==',')
{
flag++;
return NULL;
}
else
{
root=(tree*)malloc(sizeof(tree));
root->data=str[flag++];
root->left=creat(root->left);
root->right=creat(root->right);
}
return root;
}
void levelOrder(tree*root)
{
int in,out;
in=out=0;
tree*p[55];
p[in++]=root;
while(in>out)
{
if(p[out])
{
if(p[out]->left==NULL&&p[out]->right==NULL)
{
printf("%c",p[out]->data);
}
p[in++]=p[out]->left;
p[in++]=p[out]->right;
}
out++;
}
}
int main()
{
tree*root;
while(~scanf("%s",str))
{
flag=0;
root=creat(root);
levelOrder(root);
printf("\n");
}
return 0;
}