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
法一:非递归
View Code
1 #include <iostream> 2 #include <cstring> 3 #include <string> 4 using namespace std; 5 6 string s1,s2; 7 8 typedef struct node{ 9 int pt; 10 char x; 11 struct node *left; 12 struct node *right; 13 }node; 14 15 void postorder(node *root){ 16 if(root==NULL) return; 17 postorder(root->left); 18 postorder(root->right); 19 cout<<root->x; 20 } 21 22 int main() 23 { 24 int i,j; 25 26 while(cin>>s1>>s2){ 27 node *root=new(node); 28 root->left=NULL; 29 root->right=NULL; 30 root->x=s1[0]; 31 root->pt=0; 32 for(j=0;j<(int)s2.length();j++){ 33 if(s1[0]==s2[j]) 34 break; 35 } 36 root->pt=j; 37 38 for(i=1;i<(int)s1.length();i++){ 39 for(j=0;j<(int)s2.length();j++){ 40 if(s1[i]==s2[j]) 41 break; 42 } 43 node *p=root, *pre=NULL; 44 45 while(p){ 46 pre=p; 47 48 if(j<p->pt){ 49 p=p->left; 50 }else{ 51 p=p->right; 52 } 53 54 } 55 node *t=new(node); 56 t->left=NULL; 57 t->right=NULL; 58 t->x=s1[i]; 59 t->pt=j; 60 if(j<pre->pt){ 61 pre->left=t; 62 }else{ 63 pre->right=t; 64 } 65 } 66 postorder(root); 67 cout<<endl; 68 } 69 return 0; 70 }
法二:递归
View Code
1 #include<stdio.h> 2 #include<string.h> 3 void build(int n,char*s1,char*s2) 4 { 5 if(n<=0)return ; 6 int p=strchr(s2,s1[0])-s2; 7 build(p,s1+1,s2); 8 build(n-p-1,s1+p+1,s2+p+1); 9 printf("%c",s1[0]); 10 } 11 int main() 12 { 13 char s1[30],s2[30],ans[30]; 14 memset(s1,0,sizeof(s1)); 15 memset(s2,0,sizeof(s2)); 16 while(scanf("%s %s",s1,s2)!=EOF) 17 { 18 int n=strlen(s1); 19 build(n,s1,s2); 20 ans[n]='\0'; 21 printf("\n"); 22 } 23 return 0; 24 }