Description
Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes.
This is an example of one of her creations:
To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG.
She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).
Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree.
However, doing the reconstruction by hand, soon turned out to be tedious.
So now she asks you to write a program that does the job for her!
This is an example of one of her creations:
D / \ / \ B E / \ \ / \ \ A C G / / F
To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG.
She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).
Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree.
However, doing the reconstruction by hand, soon turned out to be tedious.
So now she asks you to write a program that does the job for her!
Input
The input will contain one or more test cases.
Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.)
Input is terminated by end of file.
Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.)
Input is terminated by end of file.
Output
For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root).
Sample Input
DBACEGF ABCDEFG BCAD CBAD
Sample Output
ACBFGED CDAB
#include<stdio.h> #include<string.h> #include<stdlib.h> struct node{ char value; struct node *left,*right; }; void make_tree(char *pre,char *mid,struct node *root) { int i,len; int flag=0,flag1=0; struct node *ptr1,*ptr2; char ppre[28],mmid[28]; for(i=0;mid[i]!=pre[0];i++);//找到根节点在中序中的位置 if(i!=0) //判断左子树是否存在 flag=1; if(i!=(int)strlen(mid)-1)//判断右子树是否存在 flag1=1; if(flag)//构建该根的左子树 { ptr1=(struct node *)malloc(sizeof(struct node)); ptr1->value=pre[1]; root->left=ptr1;//该根与左子树建立联系 if(flag1==0) root->right=NULL; strncpy(ppre,pre+1,i);//左子树的前序 strncpy(mmid,mid,i);//左子树的中序 ppre[i]=mmid[i]=0; make_tree(ppre,mmid,ptr1);//已知左子树的前序和中序,构建左子树 //free(ptr1); } if(flag1)//构建该根的右子树 { ptr2=(struct node *)malloc(sizeof(struct node)); ptr2->value=pre[i+1]; root->right=ptr2;// 该根与右子树建立联系 if(flag==0) root->left=NULL; len=strlen(pre)-i-1; strncpy(ppre,pre+i+1,len);//得到右子树的前序 strncpy(mmid,mid+i+1,len);//得到右子树的中序 ppre[len]=mmid[len]=0; make_tree(ppre,mmid,ptr2);//根据它的前序和中序,构建右子树 //free(ptr2); } if(strlen(pre)==1) root->left=root->right=NULL; } void visit(struct node *p)//后序遍历树 { if(p==NULL) return; visit(p->left); visit(p->right); printf("%c",p->value); } int main() { char pre[28],mid[28]; while(scanf("%s%s",pre,mid)!=EOF) { struct node *root; root=(struct node *)malloc(sizeof(struct node)); root->value=pre[0]; make_tree(pre,mid,root); visit(root); putchar('\n'); } return 0; }