标签:
D / / B E / \ / \ A C G / / F
DBACEGF ABCDEFG BCAD CBAD
ACBFGED CDAB
#include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct node { char data; struct node *rchild; struct node *lchild; }Node,*NodeTree; void rebuild(char *prime, char *mid,NodeTree &T,int len) { int index=0; if(len==0) { T=NULL; return ; } char ch=prime[0]; while(mid[index]!=ch) { index++; } T=(Node *)malloc(sizeof(NodeTree)); T->data=mid[index]; rebuild(prime+1,mid,T->rchild,index); rebuild(prime+index+1,mid+index+1,T->lchild,len-index-1); } void print(NodeTree T) { if(T!=NULL) { print(T->rchild); print(T->lchild); printf("%c",T->data); } } int main(void) { char prime[27],mid[27]; char ch; int len,n,i; NodeTree T; while(scanf("%s%s",prime,mid)!=EOF) { rebuild(prime,mid,T,strlen(mid)); print(T); printf("\n"); } return 0; }
标签:
原文地址:http://blog.csdn.net/qq_16997551/article/details/42717133