hdu 5444 构建二叉树,搜索二叉树

Elven Postman

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

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(T≤10) indicating the number of test cases.

For each test case, there is a number n(n≤1000) 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

2015 ACM/ICPC Asia Regional Changchun Online

Recommend

hujie   |   We have carefully selected several similar problems for you:  5449 5448 5447 5445 5444

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct BRT{
  int date;
  BRT *left;
  BRT *right;
  BRT(){}
  BRT(int x):date(x){
      left=NULL;
      right=NULL;
  }
};

void build_tree(BRT *root,int x){
    if(x<root->date){
         if(root->left==NULL){
              root->left=new BRT(x);
             return ;
         }
         build_tree(root->left,x);
    }
    else{
            if(root->right==NULL){
              root->right=new BRT(x);
             return ;
         }
         build_tree(root->right,x);
    }
}

void get_path(BRT *root,int x){
    if(root->date==x){
       puts("");
       return ;
    }
    else if(x>root->date){
          printf("W");
          get_path(root->right,x);
    }
    else{
    printf("E");
    get_path(root->left,x);
    }

}

int main(){
   int t;
   scanf("%d",&t);
   while(t--){
       int n;
       scanf("%d",&n);
        int x;
        scanf("%d",&x);
        BRT *root=new BRT(x);
        for(int i=1;i<n;i++){
           scanf("%d",&x);
           build_tree(root,x);
        }
        int q;
        scanf("%d",&q);

        for(int i=0;i<q;i++){
            scanf("%d",&x);
        get_path(root,x);
        }
   }
   return 0;
}
时间: 2024-11-05 14:48:13

hdu 5444 构建二叉树,搜索二叉树的相关文章

HDU 5444 Elven Postman (二叉树,暴力搜索)

题意:给出一颗二叉树的先序遍历,默认的中序遍历是1..2.……n.给出q个询问,询问从根节点出发到某个点的路径. 析:本来以为是要建树的,一想,原来不用,其实它给的数是按顺序给的,只要搜结点就行,从根开始搜,如果要到的结点比根结点大,那么一定是向W走, 然后去第一个结点,然后接着判定,一直走,如果找到结束就好.水题.当时想的有点复杂. 代码如下: #include <cstdio> #include <string> #include <cstdlib> #includ

搜索二叉树应用——简单字典实现

搜索二叉树基本概念请看上篇博客这两个问题都是典型的K(key)V(value)问题,我们用KV算法解决. 判断一个单词是否拼写正确:假设把所有单词都按照搜索树的性质插入到搜索二叉树中,我们判断一个单词拼写是否正确就是在树中查找该单词是否存在(查找key是否存在). 结构声明下 typedef char* KeyType; typedef struct BSTreeNode { struct BSTreeNode *_left; struct BSTreeNode *_right; KeyType

生成所有可能的搜索二叉树

product_all_tree(0, 5); 递归返回子树的指针集合.作为 左子树或右子树. 从而构建整颗树. #include<iostream> #include<vector> using namespace std; //double p[1000]{ 0,0.15,0.10,0.05,0.10,0.20 }; double p[1000]{ 1,2,3,4,5,6,7,8,9 }; #define N 5 class Tree { public: Tree*parent

判断一个数列是不是搜索二叉树后续遍历输出的结果

剑平面阿里被问到这个,刚开始画了下看有什么性质,乱蒙了几个都被推翻了,初始感觉就是要O(n)的,因为印象中BST的构树都可以O(nlogn)搞定.然后剑平说最后一个数肯定是根节点,一下反应过来了,就是二分出间隔点然后两边递归判断,不过这好像还是构树的思路,可以把整棵树构造出来.然后剑平说不是二分,直接遍历.不科学啊,明显的二分,然后去网上搜一下,都是遍历的,O(n^2)的吧.想了想,二分是当做合法的情况来构树的,不合法怎么判断?构造出搜索二叉树后中序遍历一下不就行了么,妥妥的O(nlogn)吧.

搜索二叉树

二叉查找树(BinarySearch Tree,也叫二叉搜索树,或称二叉排序树Binary Sort Tree)或者是一棵空树,或具有如下性质: 每个节点都有一个作为搜索依据的关键码(key),所有节点的关键码互不相同. 左子树上所有节点的关键码(key)都小于根节点的关键码(key). 右子树上所有节点的关键码(key)都大于根节点的关键码(key). 左右子树都是二叉搜索树. 二叉搜索树相关操作: (1)插入:新节点 在二叉查找树中插入新结点,要保证插入新结点后仍能满足二叉查找树的性质.插入

java递归方法建立搜索二叉树,具备查找关键字,插入新节点功能

二叉排序树的定义: 二叉排序树满足以下三个性质(BST性质): <1>若它的左子树非空,则左子树上所有节点的值均小于根节点的值 <2>若它的右子树非空,则右子树上所有节点的值均大于根节点的值 <3>左,右子树本身又各是一棵二叉排序树 根据二叉排序树的BST性质,可以说二叉排序树每个节点上的值(或称关键字)都是唯一的,并且二叉排序树以中序遍历输出的结果必然是一个有序的递增序列. 如下图所示: 用递归方法建立二叉排序树,减少了繁复的比较程序,效率较高.只需要知道每个节点的值

数据结构--‘搜索二叉树’

'二叉树'是数据结构中比较重要的一部分,这里主要讨论一下'搜索二叉树',针对'搜索二叉树的插入.删除和查找节点进行分情况讨论,希望能够帮助读者更加的理解搜索二叉树的原理. ◆搜索二叉树的性质: 1.每个节点都有一个一个作为搜索依据的关键码,所有节点的关键码都不相同. 2.左子树所有的关键码(key)都小于根节点的关键码(key). 3.右子树所有的关键码(key)都大于根节点的关键码(key). 4.左.右子树都是二叉搜索树. 实现'搜索二叉树'的节点结构可以实现为K形式,和K.V形式,若实现K

【数据结构】搜索二叉树的(递归与非递归)实现,包括:增Insert,删Remove,查Find

搜索二叉树,是二叉树一种特殊的结构. 特点: (1)每个节点都有一个关键码,并且关键码不重复. (2)左子树上的每个节点的关键码都小于根节点的关键码. (3)右子树上的每个节点的关键码都大于根节点的关键码. (4)左右子树都是搜索二叉树. 下面,为方便大家理解,我举例画一个搜索二叉树,如下: 代码见下: #define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> using namespace std; //搜索二叉树的节点结构 template&

JavaScript 二叉树搜索

TypeScript方式实现源码 1 // 二叉树与二叉树搜索 2 class Node { 3 key; 4 left; 5 right; 6 constructor(key) { 7 this.key = key; 8 this.left = null; 9 this.right = null; 10 } 11 } 12 class BinarySearchTree { 13 root = null; 14 public insert(key) { 15 let newNode = new