hdu 5444(构造二叉树然后遍历)

Elven Postman

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1286    Accepted Submission(s): 731


Problem Description
Elves are very peculiar creatures. As we all know, they can live for a very long time and their magical prowess are not something to be taken lightly. Also, they live on trees. However, there is something about them you may not know. Although delivering stuffs through magical teleportation is extremely convenient (much like emails). They still sometimes prefer other more “traditional” methods.

So, as a elven postman, it is crucial to understand how to deliver the mail to the correct room of the tree. The elven tree always branches into no more than two paths upon intersection, either in the east direction or the west. It coincidentally looks awfully like a binary tree we human computer scientist know. Not only that, when numbering the rooms, they always number the room number from the east-most position to the west. For rooms in the east are usually more preferable and more expensive due to they having the privilege to see the sunrise, which matters a lot in elven culture.

Anyways, the elves usually wrote down all the rooms in a sequence at the root of the tree so that the postman may know how to deliver the mail. The sequence is written as follows, it will go straight to visit the east-most room and write down every room it encountered along the way. After the first room is reached, it will then go to the next unvisited east-most room, writing down every unvisited room on the way as well until all rooms are visited.

Your task is to determine how to reach a certain room given the sequence written on the root.

For instance, the sequence 2, 1, 4, 3 would be written on the root of the following tree.

 
Input
First you are given an integer T(T10) indicating the number of test cases.

For each test case, there is a number n(n1000) on a line representing the number of rooms in this tree. n integers representing the sequence written at the root follow, respectively a1,...,an where a1,...,an{1,...,n}.

On the next line, there is a number q representing the number of mails to be sent. After that, there will be q integers x1,...,xq indicating the destination room number of each mail.
 
Output
For each query, output a sequence of move (E or W) the postman needs to make to deliver the mail. For that E means that the postman should move up the eastern branch and W the western one. If the destination is on the root, just output a blank line would suffice.

Note that for simplicity, we assume the postman always starts from the root regardless of the room he had just visited.
 
Sample Input
2 4 2 1 4 3 3 1 2 3 6 6 5 4 3 2 1 1 1
 
Sample Output
E WE EEEEE
 
Source
 
题意:知道一棵二叉树的中序遍历是1,2,3...n,然后给出其前序遍历,然后有q次询问,每次询问根节点到指定位置的访问路径。
题解:知道了中序遍历为1,2,3..N,那么就可以知道二叉树左边的子树权值都小于根节点,右子树的权值都大于根结点,这样的话就可以很容易的构造出一棵二叉树了,构造完之后也根据权值的大小遍历即可。
这题比较特殊,刚好是个二叉排序树。所以比较好打,下面还有一个队友写的普遍性高的模板。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=1008;
struct btree
{
    int left,right,val;
}tree[maxn];
int tot;
void init(int tot){
    tree[tot].left = tree[tot].right = -1;
}
void build(int root,int val){
    if(tree[root].left!=-1&&val<tree[root].val){ ///比根小并且左子树存在。
        build(tree[root].left,val);
    }else if(tree[root].right!=-1&&val>tree[root].val){ ///比根大并且右子树存在。
        build(tree[root].right,val);
    }else {
        init(tot);
        tree[tot].val = val;
        if(val<tree[root].val) tree[root].left = tot;
        else tree[root].right = tot;
        tot++;
    }
}
void query(int root,int val){
    if(tree[root].val==val){
        printf("
");
        return;
    }
    if(val<tree[root].val){
        printf("E");
        query(tree[root].left,val);

    }
    else {
        printf("W");
        query(tree[root].right,val);

    }
}
int v[maxn];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        tot = 1;
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&v[i]);
            if(i==1){ ///根节点
                init(tot);
                tree[tot].val = v[i];
                tot++;
            }
            else build(1,v[i]);
        }
        int q ;
        scanf("%d",&q);
        while(q--){
            int val;
            scanf("%d",&val);
            query(1,val);
        }
    }
    return 0;
}

 法二:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int maxn = 2005;
struct Node
{
    int lson,rson;
}tree[maxn];
int n,q,cnt,pre[maxn];
char path[maxn][maxn],tmp[maxn];

void build(int l,int r)
{
    if(l >= r) return;
    int pos;
    for(int i = l; i <= r; i++)
        if(pre[cnt] == i)
        {
            pos = i;
            break;
        }
    if(l != pos) ///这里要注意
        tree[pos].lson = pre[++cnt];
    build(l,pos-1);
    if(r != pos)
        tree[pos].rson = pre[++cnt];
    build(pos+1,r);
}

void dfs(int rt,int dep)
{
    if(rt == 0) return;
    strcpy(path[rt],tmp);
    tmp[dep] = 'E';
    dfs(tree[rt].lson,dep+1);
    tmp[dep] = 'W';
    dfs(tree[rt].rson,dep+1);
    tmp[dep] = 0;
}

int main()
{
    int t,u;
    scanf("%d",&t);
    while(t--)
    {
        memset(tree,0,sizeof(tree));
        scanf("%d",&n);
        for(int i = 1; i <= n; i++)
            scanf("%d",&pre[i]);
        cnt = 1;
        build(1,n);
        memset(tmp,0,sizeof(tmp));
        dfs(pre[1],0);
        scanf("%d",&q);
        while(q--)
        {
            scanf("%d",&u);
            printf("%s
",path[u]);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/liyinggang/p/5569116.html