标签:
ACBFGED ABCDEFG CDAB CBAD
DBACEGF BCAD
数据结构会遍历,会还原。。只是代码实现太弱。。
参考代码来源:http://blog.csdn.net/whjkm/article/details/39341331#comments
#include<cstdio> #include<cstdlib> #include<cstring> struct node { char value; node *lchild,*rchild; }; node * newnode(char c) { node *p=(node *)malloc(sizeof(node)); p->value=c; p->lchild=p->rchild=NULL; return p; } node *rebuild(char *post,char *in,int n) { if(n==0) return NULL; char ch=post[n-1]; node *p; p=newnode(ch); int i; for(i=0;i<n && in[i]!=ch;i++); int l_len=i; int r_len=n-i-1; if(l_len>0) p->lchild=rebuild(post,in,l_len); if(r_len>0) p->rchild=rebuild(post+l_len,in+l_len+1,r_len); return p; } void preorder(node *p) { if(p==NULL) return ; printf("%c",p->value); preorder(p->lchild); preorder(p->rchild); } int main() { char postorder[100],inorder[100]; while(scanf("%s%s",&postorder,&inorder)!=EOF) { node *tr=rebuild(postorder,inorder,strlen(postorder)); preorder(tr); printf("\n"); } return 0; }
标签:
原文地址:http://blog.csdn.net/zuguodexiaoguoabc/article/details/43907193