.
This is an es, of one of her credects:
Di
/ /
/ /
B e
/ / /
/ / /
A c g
/
/
Fly
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 IND 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 Specification
The input file 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.
Output Specification
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
#include
Using namespace std;
Char pre [27], in [27];
Void Recover (int Pi, int Pj, int II, int j) {
IF (pi 1> = pj)
Return;
INT i = II;
While (in [i]! = pre [pi])
i ;
For (int J = pi; j PRE [J] = pre [J 1]; PRE [PJ-1] = IN [I]; Recover (pi, pi (i-ii), II, i); Recover (Pi (i-II), PJ-1, I 1, IJ); } void main () { While (CIN >> Pre >> in) { INT LEN = Strlen (pre); Recover (0, Len, 0, Len); COUT << pre << endl; } }