Tree Recovery
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11452 | Accepted: 7190 |
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<cstdio> #include<cstring> //相当于一直还原根节点,到最后一层根节点即叶子节点 void Recover(char *pre,char *in,int n,char *post){ //传入参数分别为 根节点 左叶子节点 树的长度 后序的左叶子节点 if(n<=0)return; char *p=strchr(in,pre[0]);//查找根在中序中的位置 Recover(pre+1,in,(p-in),post);//递归到左子树 Recover(pre+(p-in)+1,p+1,n-(p-in)-1,post+(p-in)); //右子树 p-in 为左子树的长度 post[n-1]=pre[0];//还原根节点 } void solve(){ char pre[50],in[50],post[50]; while(scanf("%s%s",pre,in)!=EOF){ int n=strlen(pre); Recover(pre,in,n,post); post[n]='\0'; puts(post); } } int main(){ solve(); return 0; }
/* 方法二:先建树在再输出 暂时没掌握,写不出来。先贴出来吧 Time:2014-8-27 14-54 */ #include<cstdio> #include<cstring> #include<string> #include<iostream> using namespace std; typedef struct Tree{ int data; struct Tree *lChild; struct Tree *rChild; }*tree2; void Creat(tree2 &root,string pre,string in){ int len=pre.size(); if(len==0) root=NULL; else{ int p=in.find(pre[0]);//返回在 s中第一次出现的下标 if(p==-1) root=NULL; else{ root=new Tree; root->data=pre[0]; if(p==0)//碰到头,左孩子设为 空 root->lChild=NULL; else Creat(root->lChild,pre.substr(1,p),in.substr(0,p));//起始位置,终止位置 if(p==len-1)//碰到尾,右孩子设为 空 root->rChild=NULL; else Creat(root->rChild,pre.substr(p+1),in.substr(p+1)); } } } void Print(tree2 &root){ if(root==NULL) return; Print(root->lChild); Print(root->rChild); printf("%c",root->data); } void solve(){ string pre,in; while(cin>>pre>>in){ tree2 root; Creat(root,pre,in); Print(root); printf("\n"); } } int main(){ solve(); return 0; }