POJ 2255 Tree Recoery

Tree Recovery
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9354   Accepted: 5868

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

二叉树遍历问题,给出二叉树的前序遍历和中序遍历,求其后序遍历
用到了关于遍历的几个结论:
  1.给出二叉树的前序(或后序)遍历和中序遍历,则可以唯一确定其后序(或前序)遍历
  2.二叉树前序遍历的第一个值是树(或子树)的根节点,后序遍历的最后一个值是树(或子树)的根节点
  3.在二叉树的中序遍历中找到它的根节点,则根节点左边为左子树的中序遍历,根节点右边为右子树的中序遍历
代码可以用递归的方法写出,不难

 1 #include<iostream>
 2 #include<cstring>
 3 
 4 using namespace std;
 5 
 6 void f(int n,char *s1,char *s2,char *s)
 7 {
 8     for(int i=0;i<n;i++)
 9         if(s2[i]==s1[0])
10         {
11             f(i,s1+1,s2,s);
12             f(n-i-1,s1+i+1,s2+i+1,s+i);
13             s[n-1]=s1[0];
14         }
15 }
16 
17 int main()
18 {
19     char s1[30],s2[30],s[30];
20 
21     while(cin>>s1>>s2)
22     {
23         int len=strlen(s1);
24         f(len,s1,s2,s);
25         s[len]='\0';
26         cout<<s<<endl;
27     }
28 
29     return 0;
30 }
[C++]

 

posted @ 2013-05-16 20:00  ~~Snail~~  阅读(141)  评论(0编辑  收藏  举报