标签:
Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 591    Accepted Submission(s): 329

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <queue>
#include <algorithm>
using namespace std;
typedef struct node
{
    int data;
    struct node *ll;
    struct node *rr;
}Binode, *Bitree;
void creat_sort_Bitree(Bitree &root, int key)
{
    if(root==NULL){
        root=new Binode;//一种类型
        root->data=key;
        root->ll=NULL;
        root->rr=NULL;
        return ;//插入到二叉排序树上成功 返回
    }
    else{
        if(key < root->data){
            creat_sort_Bitree(root->ll, key);
        }else{
            creat_sort_Bitree(root->rr, key);
        }
    }
}
queue<char>q;
void get_aim(Bitree &root, int key)
{
    if(root->data == key){
        return;
    }else{
        if(root->data > key){
            q.push(‘E‘);
            get_aim(root->ll, key);
        }
        else{
            q.push(‘W‘);
            get_aim(root->rr, key);
        }
    }
}
int main()
{
    int tg; scanf("%d", &tg);
    int n, i, j, dd, m;
    Bitree root;
    while(tg--){
        root=NULL; //初始化root必须为空! 这个root的值在确定了根节点后是不会改变的
        scanf("%d", &n);
        while(n--){
            scanf("%d", &dd);
            creat_sort_Bitree(root, dd);
        }
        scanf("%d", &m);
        while(m--){
            scanf("%d", &dd); //在二叉排序树中将找出来
            while(!q.empty()) q.pop();//全局变量队列记得清空后在使用
            get_aim(root, dd);
            while(!q.empty()){
                printf("%c", q.front()); q.pop();
            }printf("\n");
        }
    }
    return 0;
}
2015 ACM/ICPC Asia Regional Changchun Online HDU 5444 Elven Postman【二叉排序树的建树和遍历查找】
标签:
原文地址:http://www.cnblogs.com/yspworld/p/4811282.html