abc,,de,g,,f,,,
cbegdfacgefdba35
#include<iostream> #include<cstdio> #include<cstring> using namespace std; char head[200]; int top,sum,deep; struct Tree { char c; Tree* L,*R; }; Tree* Creat() { Tree* p; p=new Tree; p->L=NULL; p->R=NULL; return p; } Tree* Build(Tree*root) { if(!head[top]||head[top]==',') return NULL; root=Creat(); root->c=head[top]; top++; root->L=Build(root->L); top++; root->R=Build(root->R); return root; } int InOrder(Tree* root,int ans) { if(!root) { if(ans>deep) { deep=ans; } return 0; } if(!root->L&&!root->R) { sum++; } InOrder(root->L,ans+1); printf("%c",root->c); InOrder(root->R,ans+1); } int PostOrder(Tree* root) { if(!root) return 0; PostOrder(root->L); PostOrder(root->R); printf("%c",root->c); } int main() { cin>>head; top=0; sum=0; deep=0; Tree* root=Build(root); InOrder(root,0); cout<<endl; PostOrder(root); cout<<endl; cout<<sum<<endl; cout<<deep<<endl; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/became_a_wolf/article/details/46662111