zzuli 1209 二叉树(2)

来源:互联网 发布:域名注册 love后缀 编辑:程序博客网 时间:2024/05/04 23:08
#include<stdio.h>#include<stdlib.h>typedef struct tree{char c;struct tree *l,*r;}t,*lt;char s[1010];int i=0,shen,mshen;lt h;lt sol(){lt cur=(lt)malloc(sizeof(t));cur->c=s[i++];if(cur->c=='1'){free(cur);return NULL;}shen++;if(shen>mshen)mshen=shen;cur->l=sol();cur->r=sol();shen--;return cur;}void ye(lt p){if(p->l==NULL&&p->r==NULL)printf("%c ",p->c);if(p->l!=NULL)ye(p->l);if(p->r!=NULL)ye(p->r);}int main(){while(gets(s)){shen=0;mshen=0;i=0;h=sol();printf("%d\n",mshen);ye(h);printf("\n");}return 0;}