标签:ack head black ios class accept turn har index.php
已知一个按先序输入的字符序列,如abd,,eg,,,cf,,,(其中,表示空结点)。请建立该二叉树并按从上到下从左到右的顺序输出该二叉树的所有叶子结点。
abd,,eg,,,cf,,, xnl,,i,,u,,
dfg uli
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 ****************************************************/
标签:ack head black ios class accept turn har index.php
原文地址:http://www.cnblogs.com/Mimick/p/6031903.html