http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2136&cid=1145
View Code
1 #include<stdio.h> 2 #include<malloc.h> 3 typedef struct node 4 { 5 char data ; 6 struct node *l, *r ; 7 }tree ; 8 tree *t ; 9 tree *creat(tree *t) 10 { 11 char c ; 12 if((c = getchar())==',') 13 return NULL ; 14 else 15 { 16 t = (tree*)malloc(sizeof(tree)) ; 17 t->data = c ; 18 t->l = creat(t->l) ; 19 t->r = creat(t->r) ; 20 } 21 return t ; 22 } 23 void midsearch(tree *t) 24 { 25 if(t!=NULL) 26 { 27 midsearch(t->l) ; 28 printf("%c", t->data) ; 29 midsearch(t->r) ; 30 } 31 } 32 void lastsearch(tree *t) 33 { 34 if(t!=NULL) 35 { 36 lastsearch(t->l) ; 37 lastsearch(t->r) ; 38 printf("%c", t->data) ; 39 } 40 } 41 int leaf(tree *t) 42 { 43 if(t==NULL) 44 return 0 ; 45 if(t->l==NULL&&t->r==NULL) 46 return 1 ; 47 else return(leaf(t->l)+leaf(t->r)) ; 48 } 49 int deep(tree *t) 50 { 51 if(t==NULL) 52 return 0 ; 53 int m, n ; 54 m = deep(t->l) ; 55 n = deep(t->r) ; 56 if(m>n) 57 return m+1 ; 58 else return n+1 ; 59 } 60 int main() 61 { 62 tree *p ; 63 int leafs ; 64 p = creat(t) ; 65 midsearch(p) ; 66 puts("") ; 67 lastsearch(p) ; 68 puts("") ; 69 leafs = leaf(p) ; 70 printf("%d\n", leafs) ; 71 printf("%d\n", deep(p)) ; 72 return 0 ; 73 }