按要求递归建树输出~

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=;
struct node {
int data;
node * left;
node * right;
};
int pre[maxn],in[maxn],T,N,q,x;
void init () {
fill (pre,pre+maxn,);
fill (in,in+maxn,);
}
node * create (int preL,int preR,int inL,int inR) {
if (preL>preR) return NULL;
node * root=new node;
root->data=pre[preL];
int k;
for (k=inL;k<=inR;k++) {
if (in[k]==pre[preL]) break;
}
int numLeft=k-inL;
root->left=create(preL+,preL+numLeft,inL,k-);
root->right=create(preL+numLeft+,preR,k+,inR);
return root;
}
void dfs (node * root,int v) {
if (root->data==v) {
printf ("\n");
return;
}
if (v<root->data) printf ("E"),dfs (root->left,v);
else printf ("W"),dfs (root->right,v);
}
int main () {
scanf ("%d",&T);
while (T--) {
scanf ("%d",&N);
for (int i=;i<=N;i++) in[i]=i;
for (int i=;i<=N;i++) scanf ("%d",&pre[i]);
node * root=create(,N,,N);
scanf ("%d",&q);
for (int i=;i<q;i++) scanf ("%d",&x),dfs (root,x);
}
return ;
}

最新文章

  1. 初识CoreData与详解
  2. ReactiveCocoa(RAC)
  3. 第一节Unity3D简介
  4. 我所了解的cgi(转)
  5. Coder-Strike 2014 - Finals (online edition, Div. 2) C题
  6. 高性能IO设计的Reactor和Proactor模式(转)
  7. No enclosing instance of type Outer is accessible. Must qualify the allocation with an enclosing instance of type Outer (e.g. x.new A() where x is an instance of Outer)
  8. .Net程序员学用Oracle系列(8):触发器、任务、序列、连接
  9. Linux中kettle自动化部署脚本
  10. UVa 11503 - Virtual Friends
  11. hdu 5391 (数论)
  12. mysql学习笔记--数据库事务
  13. 使用SSH命令行传输文件到远程服务器
  14. Cocos Creator cc.Event点击触摸事件详解
  15. 异常:unity3d ArgumentException: The Assembly System.Configuration is referenced by System.Data.
  16. Emmagee——开源Android性能测试工具
  17. MFC中UpdateData()函数的使用
  18. Python中type的用法
  19. C# unity 的 IInterceptionBehavior实现aop拦截器
  20. 〖Android〗(how-to) fix k860/k860i buletooth.

热门文章

  1. Docker - ubuntu 镜像安装网络工具
  2. ubuntu 终端快捷方式汇总
  3. 期货、期权tick数据接收
  4. IntelliJ IDEA之如何设置JVM运行参数
  5. linux服务器上部署springboot项目,并让他持续运行到后台
  6. EntityFramework 根据时间筛选数据
  7. MS Sqlserver删除字段最后的多余字符
  8. pyppeteer硬钢掉淘宝登入的滑块验证
  9. CentOS7更换阿里yum源
  10. vue路由,ajax,element-ui