码迷,mamicode.com
首页 > 其他好文 > 详细

HDU5444 Elven Postman

时间:2020-02-13 22:31:13      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:ring   postman   oid   col   create   递归   names   --   code   

按要求递归建树输出~

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=1014;
struct node  {
    int data;
    node * left;
    node * right;
};
int pre[maxn],in[maxn],T,N,q,x;
void init () {
    fill (pre,pre+maxn,0);
    fill (in,in+maxn,0);
}
node * create (int preL,int preR,int inL,int inR) {
    if (preL>preR) return NULL;
    node * root=new node;
    root->data=pre[preL];
    int k;
    for (k=inL;k<=inR;k++) {
        if (in[k]==pre[preL]) break;
    }
    int numLeft=k-inL;
    root->left=create(preL+1,preL+numLeft,inL,k-1);
    root->right=create(preL+numLeft+1,preR,k+1,inR);
    return root;
} 
void dfs (node * root,int v) {
    if (root->data==v) {
        printf ("\n");
        return;
    }
    if (v<root->data) printf ("E"),dfs (root->left,v);
    else printf ("W"),dfs (root->right,v);
}
int main () {
    scanf ("%d",&T);
    while (T--) {
        scanf ("%d",&N);
        for (int i=1;i<=N;i++) in[i]=i;
        for (int i=1;i<=N;i++) scanf ("%d",&pre[i]);
        node * root=create(1,N,1,N);
        scanf ("%d",&q);
        for (int i=0;i<q;i++) scanf ("%d",&x),dfs (root,x);
    }
    return 0;
} 

 

HDU5444 Elven Postman

标签:ring   postman   oid   col   create   递归   names   --   code   

原文地址:https://www.cnblogs.com/zhanglichen/p/12305572.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!