Tree Recovery
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9620 | Accepted: 6041 |
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)计算中根串中的根的位置root;► (2)计算左子树的规模和右子树的规模;►(3)在左子树不空的情况下,递归左子树;►(4)在右子树不空的情况下,递归右子树;#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; char preord[30],inord[30]; void recover(int preleft,int preright,int inleft,int inright) { int root,leftsize,rightsize; if(preleft<=preright&&inleft<=inright) { for(root=inleft;root<=inright;root++) if(preord[preleft]==inord[root]) break; leftsize=root-inleft; rightsize=inright-root; if(leftsize>0) recover(preleft+1,preleft+leftsize,inleft,root-1); if(rightsize>0) recover(preleft+leftsize+1,preright,root+1,inright); cout<<inord[root]; } } int main() { while(scanf("%s%s",preord,inord)==2) { int n=strlen(preord); recover(0,n-1,0,n-1); cout<<endl; } return 0; }