二叉树遍历

 

题目大意:

 

 

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 Specification 

The input file 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 Specification 

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 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 char pre[110];
 5 char mid[110];
 6 char post[110];                          //定义三个数组,表示三个序
 7 int m;
 8 void findposttree(int pre_begin,int pre_end,int mid_begin,int mid_end);
 9 int main()
10 {        
11     while (cin >> pre >> mid)
12     {
13 
14         int n = strlen(pre);
15         m = n;
16         findposttree(0, n - 1, 0, n - 1);
17         cout << post << endl;
18         memset(pre, 0, sizeof(pre));
19         memset(mid, 0, sizeof(mid));
20         memset(post, 0, sizeof(post));                      //每测一组数组记得清零,不然影响下一次的测试
21     }
22 
23     return 0;
24 
25 }
26 void findposttree(int pre_begin, int pre_end, int mid_begin, int mid_end)
27 {
28     if (pre_begin<=pre_end)                                     //树未完成的条件
29     {
30         int i;
31         for (i = mid_begin; i < mid_end; i++)
32         {
33             if (mid[i] == pre[pre_begin])                        //找树根
34                 break;
35         }
36         
37         post[--m] = pre[pre_begin];                               
38         findposttree(pre_begin+(i-mid_begin)+1, pre_end, i + 1, mid_end);                   //遍历右子树,注意算开始位置,要知道中间有多少个元素
39         findposttree(pre_begin + 1, pre_begin + (i - mid_begin) , mid_begin, i - 1);           //遍历左子树
40 
41     }
42     else
43         return;
44 
45 
46 
47 }

 

心得:

      百度学习知两序找一序的方法,第一次用程序写出来还是感觉不错的,其实个人感觉学算法就是学与教的过程,通过看书

百度学习你要的知识,然后就是教计算机去算,这其中需要你想得比较细。加油加油吧~~

 

 

posted @ 2015-07-25 08:34  白一  阅读(151)  评论(0编辑  收藏  举报