• hdu 5444 Elven Postman(根据先序遍历和中序遍历求后序遍历)2015 ACM/ICPC Asia Regional Changchun Online


    很坑的一道题,读了半天才读懂题,手忙脚乱的写完(套上模板+修改模板),然后RE到死……

    题意:

    题面上告诉了我们这是一棵二叉树,然后告诉了我们它的先序遍历,然后,没了……没了!

    反复读题,终于在偶然间注意到了这一句——"Not only that, when numbering the rooms, they always number the room number from the east-most position to the west."

    它告诉我们,东边的点总是比西边的点小——也就是说,这个树的中序遍历是从1到n的一个等差数列……

    输入:

    首行输入一个整数t,表示有t组数据;

    接下来每组数据首行一个整数n,表示有这棵树有n个节点。

    接下来一行有n个整数,表示这棵树的先序遍历。

    接下来一行一个整数m,表示m次查询。

    接下来一行有m个整数,表示每次查询的点。

    输出:

    每次查询输出从根节点到查询节点的路径,路径中,每次向左输出'E',每次向右输出'W'。

    解题思路:

    套上模板,建立起这棵树,同时用一个fm[]数组讲每个节点的父节点记录下来。然后查询的时候使用就行了。

    但是有两点需要注意:

    1. 由于我是使用指针建立的树,所以每用完一组数据都要记得释放内存!

    2. 每次第二组数据的根节点也许在第一组数据中不是根节点,所以要记得将根节点的父节点fm[root]置为0。我的现场赛名额啊……因此离我而去T_T,555555……

    不说了,说多了都是泪。。。。。

    上代码——

     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 using namespace std;
     7 
     8 struct Node
     9 {
    10     int c;
    11     Node *left;
    12     Node *right;
    13 };
    14 
    15 int fm[1010];
    16 int t, n, m;
    17 char dis[1010];
    18 int pree[1010],ine[1010];
    19 
    20 Node* BuildTree(int *pre, int *in, int length)      //建树
    21 {
    22     if(length == 0) return NULL;
    23     Node* node = (Node*) malloc(sizeof(Node));
    24     node->c = pre[0];
    25     int rootindex = -1;
    26     for(int i = 0;i < length;i++)
    27     {
    28         if(in[i] == pre[0])
    29         {
    30             rootindex = i;
    31             break;
    32         }
    33     }
    34     node->left = BuildTree(pre+1,in,rootindex);//left
    35     node->right = BuildTree(pre+1+rootindex,in+rootindex+1,length-rootindex-1);//right
    36     return node;
    37 }
    38 
    39 void print(Node *root)          //记录父节点
    40 {
    41     if(root != NULL)
    42     {
    43         if(root->left != NULL) fm[root->left->c] = (root->c)*10;
    44         print(root->left);
    45         if(root->right != NULL) fm[root->right->c] = (root->c)*10+1;
    46         print(root->right);
    47     }
    48 }
    49 
    50 void dfs(Node* root)            //释放内存
    51 {
    52     if(root != NULL)
    53     {
    54         if(root->left != NULL) dfs(root->left);
    55         if(root->right != NULL) dfs(root->right);
    56         free(root);
    57     }
    58 }
    59 
    60 int main()
    61 {
    62     //freopen("test.in", "r", stdin);
    63     scanf("%d", &t);
    64     while(t--)
    65     {
    66         scanf("%d", &n);
    67         for(int i = 0; i < n; i++)
    68         {
    69             scanf("%d", &pree[i]);
    70             ine[i] = i+1;
    71         }
    72         Node *root = BuildTree(pree, ine, n) ;
    73         fm[root->c] = 0;                //就是这里,坑死我了……我@#¥%^&*!
    74         print(root);
    75         scanf("%d",&m);
    76         for(int i = 0; i < m; i++)
    77         {
    78             int mid;
    79             scanf("%d", &mid);
    80             int k = 0;
    81             while(fm[mid] != 0)
    82             {
    83                 if(fm[mid]%10 == 1) dis[k] = 'W';
    84                 else dis[k] = 'E';
    85                 mid = fm[mid]/10;
    86                 k++;
    87             }
    88             for(int j = k-1; j >=0; j--) printf("%c", dis[j]);      //输出路径
    89             printf("
    ");
    90         }
    91         dfs(root);
    92     }
    93     return 0;
    94 }
    View Code
  • 相关阅读:
    wm_syscommand
    VC中的#pragma指令的用法
    C语言|博客作业03
    C语言|博客作业09
    C语言|博客作业07
    C语言|博客作业05
    C语言|博客作业02
    C语言|博客作业08
    C语言|博客作业06
    C语言|博客作业04
  • 原文地址:https://www.cnblogs.com/mypride/p/4814787.html
Copyright © 2020-2023  润新知