zoukankan      html  css  js  c++  java
  • POJ 2255 Tree Recovery

    Tree Recovery
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 7819   Accepted: 4947

    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 1、前序遍历的第一个字母必是 根
     3 2、在中序遍历的字母串中找出 根字母,那么根字母左右两边的字符串就分别是它的左、右子树
     4 下边是参考别人的代码写的AC代码,有一点不懂就是:
     5 为什么那个构造二叉树的函数是有返回值的,但是调用的时候直接用了 没有将返回的指针赋给任何变量,谁给解释下?
     6 */
     7 //题意:给出二叉树的先序和中序遍历,让求其后序遍历
     8 #include <iostream>
     9 #include<cstring>
    10 using namespace std;
    11 
    12 struct node
    13 {
    14     char d;
    15     node* lc;
    16     node* rc;
    17 }root[50];
    18 
    19 node* construct(node* r,char a[],char b[],int n) //构造二叉树
    20 {
    21     int i;
    22     if(n == 0) 
    23         return NULL;
    24     for (i=0;i<n;i++) 
    25     {
    26         if(b[i]==a[0]) 
    27         {
    28             r->d=a[0];
    29             r->lc=construct(&r[1], &a[1],&b[0],i);
    30             r->rc=construct(&r[i+1], &a[i+1],&b[i+1],n-i-1);
    31             break;
    32         }
    33     }
    34     return r;
    35 }
    36 
    37 void postorder(node* r) //后序遍历
    38 {
    39     if (r==NULL)
    40         return;
    41     postorder(r->lc);
    42     postorder(r->rc);
    43     cout<<char(r->d);
    44 //    printf("%c", r->d);
    45 }
    46 
    47 int main()
    48 {
    49     char a[50], b[50];
    50 
    51     while (cin>>a>>b) 
    52     {
    53         construct(&root[0],&a[0],&b[0],strlen(a));
    54         postorder(&root[0]);
    55         cout<<endl;
    56     }
    57     return 0;
    58 }
    功不成,身已退
  • 相关阅读:
    滑雪
    2084 数塔HDU
    括号匹配(二)
    项链
    单调递增最长子序列
    矩形嵌套
    最长公共子序列
    poj3253
    表达式求值
    颜色16进制代码表显示和16进制数值对比显示方便查找
  • 原文地址:https://www.cnblogs.com/dongsheng/p/2578929.html
Copyright © 2011-2022 走看看