hdu 5444(构造二叉树然后遍历)

Elven Postman

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

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

题意:知道一棵二叉树的中序遍历是1,2,3...n,然后给出其前序遍历,然后有q次询问,每次询问根节点到指定位置的访问路径。

题解:知道了中序遍历为1,2,3..N,那么就可以知道二叉树左边的子树权值都小于根节点,右子树的权值都大于根结点,这样的话就可以很容易的构造出一棵二叉树了,构造完之后也根据权值的大小遍历即可。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=1008;
struct btree
{
    int left,right,val;
}tree[maxn];
int tot;
void init(int tot){
    tree[tot].left = tree[tot].right = -1;
}
void build(int root,int val){
    if(tree[root].left!=-1&&val<tree[root].val){ ///比根小并且左子树存在。
        build(tree[root].left,val);
    }else if(tree[root].right!=-1&&val>tree[root].val){ ///比根大并且右子树存在。
        build(tree[root].right,val);
    }else {
        init(tot);
        tree[tot].val = val;
        if(val<tree[root].val) tree[root].left = tot;
        else tree[root].right = tot;
        tot++;
    }
}
void query(int root,int val){
    if(tree[root].val==val){
        printf("\n");
        return;
    }
    if(val<tree[root].val){
        printf("E");
        query(tree[root].left,val);

    }
    else {
        printf("W");
        query(tree[root].right,val);

    }
}
int v[maxn];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        tot = 1;
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&v[i]);
            if(i==1){ ///根节点
                init(tot);
                tree[tot].val = v[i];
                tot++;
            }
            else build(1,v[i]);
        }
        int q ;
        scanf("%d",&q);
        while(q--){
            int val;
            scanf("%d",&val);
            query(1,val);
        }
    }
    return 0;
}
时间: 2024-10-07 20:09:36

hdu 5444(构造二叉树然后遍历)的相关文章

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

通过二叉树的中序和后序遍历序列构造二叉树(非递归)

题目:通过二叉树的中序和后序遍历序列构造二叉树 同样,使用分治法来实现是完全可以的,可是在LeetCode中运行这种方法的代码,总是会报错: Memory Limit Exceeded ,所以这里还是用栈来实现二叉树的构建. 与用先序和后序遍历构造二叉树的方法类似,但还是要做一些改变: 如果从后往前处理中序和后序的序列,则处理就为如下所示的情况: Reverse_Post: 根-右子树-左子树 Reverse_In: 右子树-根-左子树 这样处理方式和先序-中序就差不多了,只是将添加左孩子的情况

leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)

题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)二叉树可空 2)思路:a.根据前序遍历的特点, 知前序序列(PreSequence)的首个元素(PreSequence[0])为二叉树的根(root),  然后在中序序列(InSequence)中查找此根(

leetcode题解:Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)

题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)实现与根据先序和中序遍历构造二叉树相似,题目参考请进 算法思想 中序序列:C.B.E.D.F.A.H.G.J.I 后序序列:C.E.F.D.B.H.J.I.G.A 递归思路: 根据后序遍历的特点,知道后序

根据中序遍历和后序遍历树构造二叉树

样例: 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下的树: 2 /  \ 1   3 借鉴上一篇<前序遍历和中序遍历树构造二叉树>,我们知道中序遍历为左->中->右,后序遍历为左->右->中.于是后序遍历的最后一个值即为根节点的值,根据这个值我们在中序遍历中找到根节点左子树和右子树的值,递归构造左子树和右子树即可. /** * Definition of TreeNode: * class TreeNode { * public: * in

[Leetcode] Construct binary tree from inorder and postorder travesal 利用中序和后续遍历构造二叉树

Given inorder and postorder traversal of a tree, construct the binary tree. Note:  You may assume that duplicates do not exist in the tree. 利用中序和后序遍历构造二叉树,要注意到后序遍历的最后一个元素是二叉树的根节点,而中序遍历中,根节点前面为左子树节点后面为右子树的节点.例如二叉树:{1,2,3,4,5,6,#}的后序遍历为4->5->2->6-&

LintCode(72)中序遍历和后序遍历树构造二叉树

题目 中序遍历和后序遍历树构造二叉树 根据中序遍历和后序遍历树构造二叉树 样例 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下的树: 2 /  \ 1    3 分析 递归解决. Python代码 """ Definition of TreeNode: class TreeNode: def __init__(self, val): self.val = val self.left, self.right = None, None "&q

二叉树--根据遍历构造二叉树

二叉树中的三种遍历方式,是我们最为熟知的,通过先序遍历+中序遍历或者是中序遍历+后序遍历都可以唯一确定一棵二叉树:但是注意,先序遍历+后序遍历不能确定一棵二叉树,但是如果一棵二叉树中只有度为0和度为2的节点,那么这种遍历方式也是可以确定一棵确定的二叉树的. 先序+中序–>构造二叉树 下面我们分别来看一下,根据先序+中序遍历的顺序,如何恢复一棵二叉树,代码如下: //首先采用递归的方式 public TreeNode buildTree(int[] preorder, int[] inorder)

通过树的先序和中序遍历序列来构造二叉树

题目:给出一棵二叉树的先序和中序遍历的序列,构造出该二叉树. 思路一:采用分治法. 1)取先序遍历序列的第一个值,用该值构造根结点,,然后在中序遍历序列中查找与该元素相等的值,这样就可以把序列分为三部分:左子树(如果有).根结点和右子树(如果有). 2)将两个序列都分成三部分,这样就分别形成了根结点的左子树和右子树的先序遍历和后序遍历的序列. 3)重复1)和2)步骤,直至所有结点都处理完就可以完整构成一颗二叉树了. 根据分治法构造二叉树的代码实现: TreeNode *buildTree(vec