zoukankan      html  css  js  c++  java
  • 二叉树遍历

    题目大意:

    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 }

    心得:

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

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

    ------------------------ 没有谁的人生不是斩棘前行 ---------------------------------------- JM
  • 相关阅读:
    VC计时器的一个使用心得
    从送新年礼物给外甥想到的
    对公司用人的一些思考
    [Python学习]PycURL简单学习 limodou的学习记录 limodou是一个程序员,他关心的焦点是Python, DocBook, Open Source …
    Django tips: the difference between 'blank' and 'null'
    django 单独使用数据模型
    Django application as a standalone desktop application at finally{}
    和煦: curl_setopt($ch, CURLOPT_RETURNTRANSFER,1) 使用
    [sqllite]c语言操作sqlite一: 连接sqlite数据库
    使用curl抓取网页遇到HTTP跳转时得到多个HTTP头部的问题
  • 原文地址:https://www.cnblogs.com/Lynn0814/p/4675216.html
Copyright © 2011-2022 走看看