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
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.
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.
Note that for simplicity, we assume the postman always starts from the root regardless of the room he had just visited.
4
2 1 4 3
3
1 2 3
6
6 5 4 3 2 1
1
1
WE
EEEEE
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=;
struct btree
{
int left,right,val;
}tree[maxn];
int tot;
void init(int tot){
tree[tot].left = tree[tot].right = -;
}
void build(int root,int val){
if(tree[root].left!=-&&val<tree[root].val){ ///比根小并且左子树存在。
build(tree[root].left,val);
}else if(tree[root].right!=-&&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 = ;
int n;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&v[i]);
if(i==){ ///根节点
init(tot);
tree[tot].val = v[i];
tot++;
}
else build(,v[i]);
}
int q ;
scanf("%d",&q);
while(q--){
int val;
scanf("%d",&val);
query(,val);
}
}
return ;
}
法二:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; const int maxn = ;
struct Node
{
int lson,rson;
}tree[maxn];
int n,q,cnt,pre[maxn];
char path[maxn][maxn],tmp[maxn]; void build(int l,int r)
{
if(l >= r) return;
int pos;
for(int i = l; i <= r; i++)
if(pre[cnt] == i)
{
pos = i;
break;
}
if(l != pos) ///这里要注意
tree[pos].lson = pre[++cnt];
build(l,pos-);
if(r != pos)
tree[pos].rson = pre[++cnt];
build(pos+,r);
} void dfs(int rt,int dep)
{
if(rt == ) return;
strcpy(path[rt],tmp);
tmp[dep] = 'E';
dfs(tree[rt].lson,dep+);
tmp[dep] = 'W';
dfs(tree[rt].rson,dep+);
tmp[dep] = ;
} int main()
{
int t,u;
scanf("%d",&t);
while(t--)
{
memset(tree,,sizeof(tree));
scanf("%d",&n);
for(int i = ; i <= n; i++)
scanf("%d",&pre[i]);
cnt = ;
build(,n);
memset(tmp,,sizeof(tmp));
dfs(pre[],);
scanf("%d",&q);
while(q--)
{
scanf("%d",&u);
printf("%s\n",path[u]);
}
}
return ;
}
hdu 5444(构造二叉树然后遍历)的更多相关文章
- hdu 5444 构建二叉树,搜索二叉树
Elven Postman Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- PYTHON实现算术表达式构造二叉树
LEETCOCE 224. Basic Calculator Implement a basic calculator to evaluate a simple expression string. ...
- lintcode :前序遍历和中序遍历树构造二叉树
解题 前序遍历和中序遍历树构造二叉树 根据前序遍历和中序遍历树构造二叉树. 样例 给出中序遍历:[1,2,3]和前序遍历:[2,1,3]. 返回如下的树: 2 / \ 1 3 注意 你可以假设树中不存 ...
- lintcode: 中序遍历和后序遍历树构造二叉树
题目 中序遍历和后序遍历树构造二叉树 根据中序遍历和后序遍历树构造二叉树 样例 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下的树: 2 / \ 1 3 注意 你可 ...
- [Swift]LeetCode105. 从前序与中序遍历序列构造二叉树 | 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 ...
- [Swift]LeetCode106. 从中序与后序遍历序列构造二叉树 | 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 ...
- 【2】【leetcode-105,106】 从前序与中序遍历序列构造二叉树,从中序与后序遍历序列构造二叉树
105. 从前序与中序遍历序列构造二叉树 (没思路,典型记住思路好做) 根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [ ...
- LeetCode(106):从中序与后序遍历序列构造二叉树
Medium! 题目描述: 根据一棵树的中序遍历与后序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 posto ...
- LeetCode(105):从前序与中序遍历序列构造二叉树
Medium! 题目描述: 根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inor ...
随机推荐
- SQL Server ALwayson 正在解析
原因:把主库切换到辅助副本以后,集群全部出现正在解析的情况,数据库显示“恢复挂起” 过程:把服务器重启,原以为正在解析会恢复正常.结果失败. 解决方法:出现“正在解析”的情况跟故障转移群集有关,进故障 ...
- drf 视图功能
视图 drf提供的视图功能 自己的第一次封装 #一个功能写成一个类,方便组合,只要继承它就可以有这个功能 #将功能都写在一个类中,可控性就会变差 from book.myserializers imp ...
- c++IDE
暂时使用Code::Blocks 16.01. 因为之前没有c++编译器,所以去官网选择安装codeblocks-16.01mingw-setup.exe 然后settings>Compiler ...
- poj 3187 三角数问题
题意:给你两个数,一个n表示这个三角有多少层,一个sum表示总和 思路: 类似杨辉三角 1 1 1 1 2 1 第n行的第k个数 为 n!/k!(n-k)! 暴力枚举,因 ...
- VSCode编译C/C++(一)MinGW安装配置指南
为什么不用IDE? 更加专业.轻便.其过程对于理解计算机也有更多的帮助 安装过程: 首先进入http://mingw.org/ ,点击右侧最新发布,可以下载,然后安装 点击桌面MinGWInstal ...
- HTTPS的请求与响应
HTTP和HTTPS HTTP协议(HyperText Transfer Protocol,超文本传输协议):是一种发布和接收 HTML页面的方法. HTTPS(Hypertext Transfer ...
- BZOJ 4896: [Thu Summer Camp2016]补退选
trie树+vector+二分 别忘了abs(ans) #include<cstdio> #include<algorithm> #include<vector> ...
- Pycharm Django开发(一)设置开发环境
一 由于我是一个对开发环境有强迫症的人,在装完PYTHON 2.6 3.3 3.4中,在创建Django工程的时候,会出现N个版本的python,那么在这里可以设置你喜欢和要使用的版本.
- ogre3D程序实例解析1-平移旋转与缩放
接着上篇写 http://www.cnblogs.com/songliquan/p/3294902.html 旋转 这里有必要看一下关于旋转的源代码: virtual void pitch(co ...
- 【NOIP2017】逛公园 拆点最短路+拓扑(记忆化搜索
题目描述 策策同学特别喜欢逛公园.公园可以看成一张N个点M条边构成的有向图,且没有 自环和重边.其中1号点是公园的入口,N号点是公园的出口,每条边有一个非负权值, 代表策策经过这条边所要花的时间. 策 ...