• HDU 5444 Elven Postman 二叉排序树


    HDU 5444

    题意:给你一棵树的先序遍历,中序遍历默认是1...n,然后q个查询,问根节点到该点的路径(题意挺难懂,还是我太傻逼)

    思路:这他妈又是个大水题,可是我还是太傻逼。1000个点的树,居然用标准二叉树结构来存点,,,卧槽想些什么东西。可以用一维数组,left,right直接指向位置就行了,我这里用的是链表。对于读入的序列,生成一个二叉排序树,同时记录一下路径就可以了,后面居然忘了清空path又wa了一发。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 #include<queue>
     7 #include<vector>
     8 #include<set>
     9 #include<string>
    10 #define inf 0x3f3f3f3f
    11 #define LL long long
    12 #define MAXN 2005
    13 #define IN freopen("in.txt","r",stdin);
    14 using namespace std;
    15 
    16 struct Node{
    17     int x;
    18     Node *left, *right;
    19     Node(int x = 0) :x(x){
    20         left = NULL;
    21         right = NULL;
    22     };
    23 };
    24 
    25 vector<char> path[MAXN];
    26 
    27 Node *root;
    28 void bst_insert(int x){
    29     //Node *t = root;
    30     //return;
    31     Node *t = root;
    32     //Node *s = &Node(x);
    33     while (t != NULL){
    34         if (x > t->x){
    35             path[x].push_back('W');
    36             if (t->right == NULL){
    37                 t->right = new Node(x);
    38                 break;
    39             }
    40             else{
    41                 t = t->right;
    42             }
    43         }
    44         else{
    45             path[x].push_back('E');
    46             if (t->left == NULL){
    47                 t->left = new Node(x);
    48                 break;
    49             }
    50             else{
    51                 t = t->left;
    52             }
    53         }
    54     }
    55 }
    56 
    57 int main(){
    58     //IN;
    59     //freopen("out.txt", "w", stdout);
    60     int t, n;
    61     scanf("%d", &t);
    62     while (t--){
    63         scanf("%d", &n);
    64         int x;
    65         scanf("%d", &x);
    66         root = new Node(x);
    67         for (int i = 1; i <= n; i++){
    68             path[i].clear();
    69         }
    70         for (int i = 2; i <= n; i++) {
    71             scanf("%d", &x);
    72             bst_insert(x);
    73         }
    74         //    find(1, n, 1);
    75         int q;
    76         scanf("%d", &q);
    77         //bfs();
    78         while (q--){
    79             scanf("%d", &x);
    80             for (int i = 0; i < path[x].size(); i++){
    81                 printf("%c", path[x][i]);
    82             }
    83             printf("
    ");
    84         }
    85     }
    86     return 0;
    87 }
  • 相关阅读:
    为知笔记使用备注
    困惑激发的正能量
    再多坚持一会,相信自己就好!
    看博文《前路漫漫,何为终点?》的一点小感想
    前端的杂谈
    JS 客户端检测
    DOM
    JavaScript & XML
    我所认识的XPath
    Javascript 面向对象编程
  • 原文地址:https://www.cnblogs.com/macinchang/p/4808299.html
Copyright © 2020-2023  润新知