现在的位置: 首页 > 综合 > 正文

POJ_2255

2018年04月13日 ⁄ 综合 ⁄ 共 2325字 ⁄ 字号 评论关闭

一.题目

Tree Recovery
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 11581
Accepted: 7268

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: 

                                               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. 

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


二.解题技巧

    普通的根据先序和中序推断后序的过程,因为二叉树中的字母只出现一次,所以,推断出来的后序也是唯一的。主要解题思路,根据先序,可以知道先序中出现的是整个树的根,然后在中序中找到根的位置。在中序中,根的左边部分即为左子树,右边部分即为右子树。在先序中,从根之后的第一个元素开始,个数和中序中的个数一致,剩下的部分即为右子树。得到了左子树和右子树后,就可以将左子树和右子树分别当做两个树重复进行上面的操作。因此,可以采用递归实现,递归结束的条件是,当长度为0或1的时候,当为0的时候,不进行操作,当为1的时候,直接输出字符。


三.实现代码

#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

void Deal(const char* Preorder, const char* Inorder, int Length)
{
    switch(Length)
    {
    case 0:
        break;
    case 1 :
        cout << (*Preorder);
        break;
    default:
        const char Root = Preorder[0];
        const char* RootIndex = find(Inorder, Inorder + Length, Root);
        int Index = RootIndex - Inorder;
        
        // left subtree
        Deal(Preorder + 1, Inorder, Index);
        
        // right subtree
        Deal(Preorder + 1 + Index, RootIndex + 1, Length - Index - 1);
        
        cout << Root;
        break;
    }



}

int main()
{
    string Preorder;
    string Inorder;

    while(cin >> Preorder)
    {
        cin >> Inorder;
        Deal(Preorder.c_str(), Inorder.c_str(), Preorder.size());
        cout << endl;
    }
    return 0;
}


四.体会

   本来觉得这道题很难的,后面观察了一下,就发现了上面的规律,看来,做题之前还是要先观察下比较好。



版权所有,欢迎转载,转载请注明出处,谢谢微笑

【上篇】
【下篇】

抱歉!评论已关闭.