Dbacegfbcad
Idea: To see the first thought of the subject is to reconstruct a binary tree, by the middle sequence traversal and post-order traversal can reconstruct a binary tree, and then the first sequence traversal output results
The code is as follows:
#include <stdio.h> #include <stdlib.h> #include <string.h>typedef struct node{char num;struct node * Lchild,*rchild;} Node,*pnode;void Buildtree (Pnode * root,char*post,char*in,int len)/*root represents the root node, post represents the result of the order traversal, in represents the result of the middle sequence traversal, Len indicates the length of the current subtree */{if (len==0) {*root=null; return;} Pnode node= (pnode) malloc (sizeof (node)); Node->num=post[len-1];node->lchild=node->rchild=null;*root=node ; Char *s=strchr (in,node->num);//Find the location of the root node in the middle sequence traversal int leftlen=strlen (in)-strlen (s);//Zuozi length int rightlen= len-leftlen-1;//the length of the right subtree Buildtree (& (*root)->lchild,post,in,leftlen);//recursive construction left subtree Buildtree (& (*root) Rchild,post+leftlen,s+1,rightlen);//recursively establish right subtree}void bianlitree (pnode root)//First Order Traversal {if (root==null) return;p rintf ("%c", Root->num); Bianlitree (Root->lchild); Bianlitree (root->rchild);} int main () {char post[26],in[26]; while (scanf ("%s%s", post,in)!=eof) {Pnode root=null;buildtree (&root,post,in,strlen (in)); Bianlitree (root); printf ("\ n");} return 0;}
After the AC to see the Code of the Great God Instant tears Ben too simple to rebuild the binary tree, only need to get the results of each output can!
Great God Code:
#include <stdio.h> #include <string.h>void ReBuild (char *pre, char *in,char *post, int len) {if (len>0) {int N =STRCHR (in,post[len-1])-in; ReBuild (Pre+1,in,post,n); ReBuild (pre+n+1,in+n+1,post+n,len-n-1);p re[0]=post[len-1];}} int main () {char pre[30],in[30],post[30];int len;while (~scanf ("%s%s", Post,in)) {Len=strlen (POST); ReBuild (Pre,in,post,len);p re[len]=0;puts (pre);}}