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 <algorithm>
#include <string.h>
#include <stdio.h>
#include <iostream>
#define N 30
using namespace std;
struct tree
{
char data;
struct tree *right, *left;
};
struct tree *create ( int len, char *Prch, char *Inch )
{
struct tree *root;
int i;
root = (struct tree *)malloc(sizeof (struct tree));
/*That is no charactors to convert
已经没有字符可以转化了*/
if ( len == 0 )
{
return root = NULL;
}
root->data = Prch[0];
/*Find the position of the first charactors string in the Inch
找到第一个字符所在Inch的位置*/
for ( i = 0; i < len ; i++ )
{
if ( Prch[0] == Inch[i] )
{
break;
}
}
/*The most important part 最重要的部分*/
root->left = create ( i, Prch+1, Inch );
root->right = create ( len-i-1, Prch+i+1, Inch+i+1 );
return root;
};
void AFTER_order ( struct tree *root )
{
if ( root )
{
AFTER_order ( root->left );
AFTER_order ( root->right );
printf ( "%c", root->data );
}
}
int main()
{
char Inch[N], Prch[N];
while ( ~scanf ( "%s %s", Prch, Inch ) )
{
struct tree *root;
int len = strlen( Inch );
/*established two forks tree 建立二叉树*/
root = create ( len, Prch, Inch );
/*After order output 后序输出*/
AFTER_order ( root );
printf ( "\n" );
}
}
代码菜鸟,如有错误,请多包涵!!!