zoukankan      html  css  js  c++  java
  • POJ 2255 Tree Recovery 解题心得

    原题:

    Description

    Download as PDF
     

    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<cstdio>
      3 #include<queue>
      4 #include<cstring>
      5 using namespace std;
      6 
      7 
      8 
      9 
     10 
     11 char preorder[30];
     12 char inorder[30];
     13 queue<char> post;
     14 
     15 struct tree
     16 {
     17     char date;
     18     tree * lchild;
     19     tree * rchild;
     20     //tree():date(''),lchild(NULL),rchild(NULL){}
     21 };
     22 
     23 
     24 
     25 tree * buildtree(char pre[], char in[], int n)
     26 {
     27     if (n == 0)
     28         return NULL;
     29     else
     30     {
     31         int Lnum, Rnum;
     32         tree *p = new tree();
     33         p->date = pre[0];
     34         for (int i = 0; i < n; i++)
     35         {
     36             if (in[i] == pre[0])
     37             {
     38                 Lnum = i;
     39                 Rnum = n - 1 - i;
     40                 break;
     41             }
     42         }
     43         p->lchild = buildtree(pre + 1, in, Lnum);
     44         p->rchild = buildtree(pre + Lnum + 1, in + Lnum + 1, Rnum);
     45         return p;
     46     }
     47 
     48 }
     49 
     50 
     51 void postorder(tree *pR)
     52 {
     53     if (pR == NULL)
     54     {
     55         return;
     56     }
     57     postorder( pR->lchild);
     58     postorder( pR->rchild);
     59     post.push(pR->date);
     60     return;
     61 }
     62 
     63 void deletetree (tree * p)
     64 {
     65     if (p == NULL)
     66     {
     67         return;
     68     }
     69     deletetree(p->lchild);
     70     deletetree(p->rchild);
     71     delete(p);
     72     return;
     73 }
     74 
     75 int main()
     76 {
     77     while (cin >> preorder)
     78     {
     79         
     80         cin >> inorder;
     81         tree t;
     82         tree *pRoot=buildtree(preorder, inorder, strlen(inorder));
     83         postorder(pRoot);
     84         deletetree(pRoot);
     85         while (1)
     86         {
     87             if (!post.empty())
     88             {
     89                 cout << post.front();
     90                 post.pop();
     91             }
     92             else
     93             {
     94                 cout << endl;
     95                 break;
     96             }
     97         }
     98     }
     99 
    100 
    101     return 0;
    102 }
  • 相关阅读:
    Linux虚拟机突然不能上网了
    项目经验不丰富、技术不突出的程序员怎么打动面试官?
    10分钟看懂!基于Zookeeper的分布式锁
    BATJ等大厂最全经典面试题分享
    分享30道Redis面试题,面试官能问到的我都找到了
    一个六年Java程序员的从业总结:比起掉发,我更怕掉队
    我是这样手写 Spring 的(麻雀虽小五脏俱全)
    自述:为什么一部分大公司还在采用过时的技术,作为技术人而言该去大公司还是小公司
    Java精选面试题之Spring Boot 三十三问
    Java程序员秋招面经大合集(BAT美团网易小米华为中兴等)
  • 原文地址:https://www.cnblogs.com/shawn-ji/p/4678405.html
Copyright © 2011-2022 走看看