SDUT 3346 数据结构实验之二叉树七:叶子问题

数据结构实验之二叉树七:叶子问题

Time Limit: 1000MS Memory Limit: 65536KB

Problem Description

已知一个按先序输入的字符序列,如abd,,eg,,,cf,,,(其中,表示空结点)。请建立该二叉树并按从上到下从左到右的顺序输出该二叉树的所有叶子结点。

Input

 输入数据有多行,每一行是一个长度小于50个字符的字符串。

Output

 按从上到下从左到右的顺序输出二叉树的叶子结点。

Example Input

abd,,eg,,,cf,,,
xnl,,i,,u,,

Example Output

dfg
uli

DQE:

水题
 
 1 #include <iostream>
 2 #include <cstdio>
 3 
 4 using namespace std;
 5 
 6 struct Tree
 7 {
 8     char c;
 9     Tree *lt,*rt;
10 };
11 
12 Tree *creat(char *&xx)
13 {
14     if(*xx=='\0')
15         return NULL;
16     if(*xx==',')
17     {
18         xx++;
19         return NULL;
20     }
21     Tree *r=new Tree;
22     r->c=*xx++;
23     r->lt=creat(xx);
24     r->rt=creat(xx);
25     return r;
26 }
27 
28 void cxvisit(Tree *r)
29 {
30     Tree *que[100];
31     int i=0,j=0;
32     que[j++]=r;
33     while(i<j)
34     {
35         if(que[i])
36         {
37             que[j++]=que[i]->lt;
38             que[j++]=que[i]->rt;
39             if(que[i]->lt==NULL&&que[i]->rt==NULL)
40                 printf("%c",que[i]->c);
41         }
42         i++;
43     }
44 }
45 
46 int main()
47 {
48     char xx[55],*p;
49     Tree *root;
50     while(scanf("%s",xx)!=EOF)
51     {
52         p=xx;
53         root=creat(p);
54         cxvisit(root);
55         printf("\n");
56     }
57     return 0;
58 }
59 
60 /***************************************************
61 User name: ***
62 Result: Accepted
63 Take time: 0ms
64 Take Memory: 156KB
65 Submit time: 2016-11-03 18:43:40
66 ****************************************************/

 

posted @ 2016-11-04 22:24  Leroscox  阅读(277)  评论(0编辑  收藏  举报