hdu5444-Elven Postman-最最朴素的二叉搜索树

来源:互联网 发布:新手淘宝买什么摄影棚 编辑:程序博客网 时间:2024/05/29 10:09

就是裸的二叉搜索树

只需要在插入的时候 给节点附加一个W或E的信息


查询时输出该信息就可以


这么水的题居然没看到。。。

#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <cstdio>#include <algorithm>#include <iostream>#include <queue>#include <set>#include <vector>#define  inf 0x7fffffff#define lson l , m , rt << 1#define rson m + 1 , r , rt << 1 | 1const int maxn = 50005;struct node{int val;node *l,*r;char p;node (){l=r=NULL;}};class Btree{public:   void insert(int& sb,node* &hd,int p){if (hd==NULL){hd=new node;hd->val=sb; if (p){if (p==1) hd->p='E';elsehd->p='W';}}else{if (sb<=hd->val)insert(sb,hd->l,1);elseinsert(sb,hd->r,2);}}int query(int& sb,node *hd){if (hd->val==sb){printf("%c",hd->p);return 0;}else{if(hd->p)printf("%c",hd->p); if (sb<=hd->val)query(sb,hd->l);elsequery(sb,hd->r);} }} ;node *head=NULL;Btree tp;int tm[1005];int main(void){int t,n,i ;scanf("%d",&t);while(t--){head=NULL;  scanf("%d",&n);int tmp;  for (i=1;i<=n;i++){scanf("%d",&tm[i]); tp.insert(tm[i], head,0);}head->p=0;int m;scanf("%d",&m);for (i=1;i<=m;i++){scanf("%d",&tmp);if (tmp!=tm[1])  tp.query(tmp,head);printf("\n");}}    return 0;} 


0 0