转载 http://blog.csdn.net/crescent_star/article/details/5494637
Tree Recovery
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8859 | Accepted: 5585 |
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 <iostream>
#include <stdio.h>
#include <stack>
using namespace std;
// 递归函数
// 参数表:前序遍历字符串,此次处理的前序字符串的起始位置,此次处理的前序字符串的终止位置
// 中序遍历字符串,此次处理的中序字符串的起始位置,此次处理的中序字符串的终止位置
// 输出栈
void InsertIntoPostord( char* m_Preord, int m_Preord_Beginpos, int m_Preord_Endpos,
char* m_Inord, int m_Inord_Beginpos, int m_Inord_Endpos,
stack<char> &m_Stack_Postord )
{
// 循环变量
int m_Cycle;
// 如果 begin == end,说明只剩最后一个,放入栈中,如果 begin > end,不处理
// 如果 begin < end,进行递归处理
if ( m_Preord_Beginpos == m_Preord_Endpos )
m_Stack_Postord.push( m_Preord[ m_Preord_Beginpos ] );
else if (m_Preord_Beginpos < m_Preord_Endpos)
{
// 前序的 [ m_Preord_Beginpos ],这是根结点,放入栈
m_Stack_Postord.push( m_Preord[ m_Preord_Beginpos ] );
// 在中序中寻找根结点的位置
for ( m_Cycle = m_Inord_Beginpos; m_Cycle <= m_Inord_Endpos; ++ m_Cycle )
{
if ( m_Inord[ m_Cycle ] == m_Preord[ m_Preord_Beginpos ] )
break;
}
// 递归处理中序中根结点之后的数据
InsertIntoPostord( m_Preord, m_Preord_Beginpos + m_Cycle - m_Inord_Beginpos + 1, m_Preord_Endpos,
m_Inord, m_Cycle + 1, m_Inord_Endpos, m_Stack_Postord );
// 递归处理中序中根结点之前的数据
InsertIntoPostord( m_Preord, m_Preord_Beginpos + 1, m_Preord_Beginpos + m_Cycle - m_Inord_Beginpos,
m_Inord, m_Inord_Beginpos, m_Cycle - 1, m_Stack_Postord );
}
}
int main()
{
// 前序 中序
char m_Preord[ 27 ], m_Inord[ 27 ];
// 前序长度,中序长度
int m_Length_Preord, m_Length_Inord;
// 初始化
memset( m_Preord, 0, 27 );
memset( m_Inord, 0, 27 );
// 栈
stack<char> m_Stack_Postord;
// 循环输入
while ( scanf( "%s %s", m_Preord, m_Inord ) != EOF )
{
// 得到前序中序的长度,字符串的长度
m_Length_Preord = strlen( m_Preord );
m_Length_Inord = strlen( m_Inord );
// 递归函数,插入栈
InsertIntoPostord( m_Preord, 0, m_Length_Preord - 1, m_Inord, 0, m_Length_Inord - 1, m_Stack_Postord );
// 输出
while ( !m_Stack_Postord.empty() )
{
printf( "%c", m_Stack_Postord.top() );
m_Stack_Postord.pop();
}
printf( "/n");
}
system( "pause" );
return 0;
}