Tree Recovery
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13374 | Accepted: 8348 |
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
Source
题目大意:二叉树的层次遍历;给你前序和中序遍历,让你求出后序遍历。
思路:对于一颗二叉树的层次遍历:
前序遍历:根-左子树-右子树;
中序遍历:左子树-根-右子树;
后序遍历:左子树-右子树-根;
从中我们可以得出结论,前序遍历的第一个节点一定是根节点,而且这个根节点在中序遍历的左边是左子树部分,在右边是右子树部分。
对于我们构建思路:在中序遍历中先找到前序遍历里边第一个节点,左边就是左子树,右边就是右子树。然后递归寻找解,我们不妨拿样例做实验:
我们以第一个样例为例:
DBACEGF ABCDEFG
在中序遍历中找到D的位子记录D:3.然后我们优先找右子树:
EGF EFG在中序遍历中找到E的位子记录E:0,,然后我们继续优先找右子树:
GF FG 在中序遍历中找到G的位子记录G:1,然后发现不能继续找右子树,我们这个时候找左子树:
F,FG,记录F,这个时候我们发现不能遍历任何子树,回溯,发现途中都没有能进行的左子树dfs,这个时候回溯到顶,再进行左子树的dfs。
DBACEGF ABCDEFG我们遍历左子树,过程比较复杂,这里就不一一列举了,直接根据AC代码来学习即可:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
char a[50];char b[50];
int vis[50];
int cont=0;
char ans[50];
void dfs(char a[],char b[])
{
if(vis[a[0]-'A']==1)return ;
//printf("%s %s\n",a,b);
ans[cont]=a[0];cont++;
int p=strchr(b,a[0])-b;
if(isupper(a[p+1])&&isupper(b[p+1]))dfs(a+p+1,b+p+1);
if(isupper(a[1]))dfs(a+1,b);
vis[a[0]-'A']=1;
}
int main()
{
while(~scanf("%s%s",a,b))
{
memset(vis,0,sizeof(vis));
cont=0;
dfs(a,b);
for(int i=cont-1;i>=0;i--)
{
printf("%c",ans[i]);
}
printf("\n");
}
}