Tree Recovery
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5996 | Accepted: 3932 |
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
/ \
/ \
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
Ulm Local 1997
本来听学长讲的是用递归做的,可是做着做着就变成了非递归程序了,其实原理很简单,就是遇到在str2中的位置大于当前位置则向又,否则向左,直到为空时,赋值
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char str1[30],str2[30];
typedef struct tree{
int data;
struct tree *left,*right;
}node,*Lnode;
int m=sizeof(node);
Lnode newnode()
{
Lnode p;
p=(Lnode)malloc(m);
p->left=NULL;
p->right=NULL;
return p;
}
int find(char s)
{
int i;
for(i=0;str2[i];i++)
if(str2[i]==s)return i;
return 0;
}
void post(Lnode p)
{
if(p==NULL)return;
post(p->left);
post(p->right);
printf("%c",str2[p->data]);
}
int main()
{
Lnode root,p,s;
int i,flag;
while(scanf("%s %s",str1,str2)!=EOF)
{
root=newnode();
root->data=find(str1[0]);
for(i=1;str1[i];i++)
{
p=newnode();
p=root;
while(1){
if(find(str1[i])>p->data)
{
if(p->right==NULL)
{flag=1;
break;}
else p=p->right;
}
else {
if(p->left==NULL)
{flag=2;
break;}
else
p=p->left;
}
}
s=newnode();
s->data=find(str1[i]);
if(flag==1)p->right=s;
else p->left=s;
}
post(root);
printf("\n");
}
return 0;
}
本来听学长讲的是用递归做的,可是做着做着就变成了非递归程序了,其实原理很简单,就是遇到在str2中的位置大于当前位置则向又,否则向左,直到为空时,赋值
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char str1[30],str2[30];
typedef struct tree{
}node,*Lnode;
int m=sizeof(node);
Lnode newnode()
{
}
int find(char s)
{
}
void post(Lnode p)
{
}
int main()
{
}