poj2255 Tree Recovery
Tree Recovery
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4273 | Accepted: 2852 |
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
1 #include <cstdlib> 2 #include <iostream> 3 #include <cstdio> 4 #include <algorithm> 5 #include <string.h> 6 #include <cmath> 7 using namespace std; 8 char pre[26],in[26]; 9 10 struct Node { 11 char data; 12 int lchild,rchild; 13 }binode[26]; 14 int mount=1; 15 16 17 18 void creat_bitree(int head,int pre_l,int pre_h,int in_l,int in_h) 19 { 20 int i,k; 21 for(i=in_l;i<=in_h;i++) 22 if(in[i]==pre[pre_l]) 23 break; 24 binode[head].data=in[i]; 25 if(i==in_l) 26 binode[head].lchild=-1; 27 else 28 {k=mount;mount++;binode[head].lchild=k;creat_bitree(k,pre_l+1,pre_l+i-in_l,in_l,i-1);} 29 30 if(i==in_h) 31 binode[head].rchild=-1; 32 else 33 {k=mount;mount++;binode[head].rchild=k;creat_bitree(k,pre_l+i-in_l+1,pre_h,i+1,in_h);} 34 } 35 36 int num=0; 37 38 39 40 void post_order(int head) 41 { 42 if(binode[head].lchild!=-1) 43 post_order(binode[head].lchild); 44 45 46 if(binode[head].rchild!=-1) 47 post_order(binode[head].rchild); 48 cout<<binode[head].data; 49 50 } 51 52 53 54 55 int main(int argc, char *argv[]) 56 { 57 while(cin>>pre>>in) 58 {mount=1; 59 num=0; 60 int pre_l=0, pre_h=strlen(pre)-1,in_l=0, in_h=strlen(in)-1; 61 62 creat_bitree(0,pre_l,pre_h,in_l,in_h); 63 post_order(0); 64 cout<<endl; 65 } 66 67 68 system("PAUSE"); 69 return EXIT_SUCCESS; 70 }