Tree Recovery
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13448 | Accepted: 8393 |
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
题意:
给出一个二叉树的先序和中序的遍历结果,求解这棵树后续遍历的结果
题解:
数的前中后三种遍历方式可以用递归实现,进行分解的时候,也是可以用递归实现的
先序遍历的第一个元素是父节点,找到在中序遍历的这个点的位置,将中序序列分成两部分,对非空的子树进行递归
ps:数据结构没学好,到现在只记得大概的做法了,真心做的时候,发现自己代码水平真不行,想到的东西也很难敲出来....
/*
http://blog.youkuaiyun.com/liuke19950717
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1005;
char a[maxn],b[maxn];
int x[maxn];//辅助数组
void dfs(int la,int ra,int lb,int rb)
{
int i=x[a[la]-'A'];//定位节点的位置
int j=i-lb;//当前节点的左子树的字符长度
int k=rb-i;//当前节点的右子树的字符长度
if(j)
{
dfs(la+1,la+j,lb,i-1);//递归左子树
}
if(k)
{
dfs(la+j+1,ra,i+1,rb);//递归右子树
}
printf("%c",a[la]);//输出的次序就是后续遍历的结果
}
int main()
{
while(~scanf("%s%s",a,b))
{
int len=strlen(a);
for(int i=0;i<len;++i)
{
x[b[i]-'A']=i;
//数组里保存中序的每个字母的下标
}
dfs(0,len-1,0,len-1);
printf("\n");
}
return 0;
}