//a(b(d,e(h(j,k(l,m(,n))))),c(f,g(,i))) //data source exp113 #include<iostream> using namespace std; const int SIZE=10000; struct BTNode{ char data; struct BTNode * lchild,*rchild; }; void createBTree(BTNode * &bt,char *str){ BTNode* st[SIZE],*p=NULL; int top=-1,k,j=0; char ch; bt=NULL; ch=str[j]; while(ch){ switch(ch){ case '(':top++;st[top]=p;k=1;break; case ')':top--; break; case ',':k=2;break; default: p=(BTNode *)malloc(sizeof(BTNode)); p->data=ch;p->lchild=p->rchild=NULL; if(bt==NULL) bt=p; else{ switch(k){ case 1:st[top]->lchild=p;break; case 2:st[top]->rchild=p;break; } } } j++; ch=str[j]; } } void display(BTNode * bt){ if(bt!=NULL){ cout<<bt->data; if(bt->lchild!=NULL || bt->rchild!=NULL){ cout<<"("; display(bt->lchild); if(bt->rchild!=NULL) cout<<","; display(bt->rchild); cout<<")"; } } //cout<<endl; } int LeafCount(BTNode * bt){ int num1,num2; if(bt==NULL) return 0; else if(bt->lchild==NULL && bt->rchild==NULL) return 1; else{ num1=LeafCount(bt->lchild); num2=LeafCount(bt->rchild); return num1+num2; } } int SngLeafCount(BTNode * bt){ int num1,num2; if(bt==NULL) return 0; else if(bt->lchild==NULL && bt->rchild!=NULL){ num1=SngLeafCount(bt->rchild); return num1+1; } else if(bt->lchild!=NULL && bt->lchild==NULL){ num1=SngLeafCount(bt->lchild); return num1+1; } else{ num1=SngLeafCount(bt->lchild); num2=SngLeafCount(bt->rchild); return num1+num2; } } int main(){ char str[100]; cin>>str; BTNode* bt; createBTree(bt,str); display(bt); cout<<endl; cout<<LeafCount(bt)<<endl; cout<<SngLeafCount(bt)<<endl; }