hdu 5444 Elven Postman

来源:互联网 发布:通讯网络与设备是什么 编辑:程序博客网 时间:2024/05/16 17:34

题目要求我们建立一棵二叉搜索树,然后每次从根出发,走的路径记录下来

数据结构


#include<bits/stdc++.h>using namespace std;struct point{    int v;    point * lson;    point * rson;};point *root=NULL;int k;char path[10010];void searchBST(point *p,int x){    if(p->v==x)    {          return;    }    else    {          if(p->v<x)          {                path[k++] = 'W';                 searchBST(p->rson,x);                return ;          }          else           {                path[k++] = 'E';                searchBST(p->lson,x);                return;          }    }    return ;}void insertBST(int x,point * p){    if(p==NULL)    {          root = new point;          root->v = x;          root->lson = NULL;          root->rson = NULL;          return ;    }    else if(p!=NULL)    {        if(x<p->v)        {              if(p->lson == NULL)              {                    p->lson = new point;                    p->lson->v = x;                    p->lson->lson = NULL;                    p->lson->rson = NULL;                    return ;              }              else              {                    insertBST(x,p->lson);                    return ;              }        }        else        {              if(p->rson == NULL)              {                    p->rson = new point;                    p->rson->v = x;                    p->rson->lson = NULL;                    p->rson->rson = NULL;                    return ;              }              else              {                    insertBST(x,p->rson);                    return;              }        }    }    return;}int main(){      int t;      cin>>t;      while(t--)      {            root = NULL;            int n;            cin>>n;            int i,j;            int x;            for(i=0;i<n;i++)            {                  cin>>x;                  insertBST(x,root);            }            int m;            cin>>m;            for(i=0;i<m;i++)            {                cin>>x;                k = 0;                searchBST(root,x);                if(k==0)                {                    cout<<endl;                }                else                {                      for(j=0;j<k;j++)                          cout<<path[j];                      cout<<endl;                }            }      }      return 0;}


0 0