这是一题二叉树遍历的典型题,告诉我们中序遍历和另外一种遍历序列,然后求任何一种遍历序列。

这题的核心

  1. 建树
  2. BFS
#include<bits/stdc++.h>
using namespace std;
const int MAXN=35;
int post[MAXN];
int in[MAXN];
int pre[MAXN];
int n;
struct node{
int data;
node* lchild;
node* rchild;
};
node* create(int postL,int postR,int inL,int inR){
if(postL>postR)return NULL;
node* root=new node;
root->data=post[postR];
int k;
for(k=inL;k<=inR;k++){
if(in[k]==post[postR])break;
}
int numLeft=k-inL;
root->lchild=create(postL,postL+numLeft-1,inL,k-1);
root->rchild=create(postL+numLeft,postR-1,k+1,inR);
return root;
}
int num=0;
void BFS(node* root){
queue<node*>q;
q.push(root);
while(!q.empty()){
node* now=q.front();
q.pop();
printf("%d",now->data);
num++;
if(num<n)printf(" ");
if(now->lchild!=NULL)q.push(now->lchild);
if(now->rchild!=NULL)q.push(now->rchild);
}
}
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",post+i);
}
for(int i=0;i<n;i++){
scanf("%d",in+i);
}
node* root=create(0,n-1,0,n-1);
BFS(root);
return 0;
}

A1020. Tree Traversals(25)的更多相关文章

  1. 【PAT】1020 Tree Traversals (25)(25 分)

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  2. pat1020. Tree Traversals (25)

    1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...

  3. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  4. PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  5. PAT Advanced 1020 Tree Traversals (25 分)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  6. PAT A1020 Tree Traversals (25 分)——建树,层序遍历

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  7. A1020 Tree Traversals (25 分)

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  8. PAT A1020 Tree Traversals(25)

    题目描述 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder ...

  9. 【PAT甲级】1020 Tree Traversals (25 分)(树知二求一)

    题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...

随机推荐

  1. 常用js对象、数组、字符串的方法

    字符串charAt() 返回在指定位置的字符.charCodeAt() 返回在指定的位置的字符的 Unicode 编码.concat() 连接字符串.indexOf() 检索字符串.match() 找 ...

  2. 软工实践——团队作业需求规格说明书——原型UI设计

    登录界面 还包含忘记密码和注册的功能 注册界面 注册成功后会有弹窗提示,且一个手机号只能注册一次. 忘记密码界面 通过手机收到的验证码更改密码. 项目界面 登陆之后的页面就是这个项目界面.在拥有的界面 ...

  3. IE8 input X 去掉文本框的叉叉和密码输入框的眼睛图标

    从IE 10开始,type="text" 的 input 在用户输入内容后,会自动产生一个小叉叉(X),方便用户点击清除已经输入的文本 对于type="password& ...

  4. 网络嗅探与欺骗(FTP部分)——P201421410029

    第三部分 FTP协议分析 1. 两个同学一组,A和B. 2.A同学架设FTP服务器,并设置用户名和密码,例如wbx /wbx 3.B同学在机器中安装Wireshark,并将其打开:之后用用户名和密码登 ...

  5. Android使用AsyncTask设置请求超时的注意事项

    备注:该篇文章为原创,转载请声明地址,谢谢! /** * AsyncTaskTools2集成了AsyncTask类 * 前三个参数为回到函数,最后一个为全局的Context */ final Asyn ...

  6. jqgrid 自定义添加行数据

    一般在设置了自定义按钮后,比如‘添加’按钮,点击添加需要添加一条数据在表格中. 通过jqgrid的方法 addRowData 插入一行数据. //添加一行数据 function addRow() { ...

  7. STS-创建spring配置文件

    1.创建一个bean文件 2.输入文件名applicationContext.xml 3.这里会自动显示模板文件 4.创建后,自动填充头不定义 到这里就可以发现,我们创建spring文件时,需要的配置 ...

  8. 怎样让oracle实验本在不做实验时性能提升——win7下举例

    怎样让oracle实验本在不做实验时性能提升--win7下举例 型号:ThinkPad E431 系统:WIN7 实验使用的笔记本不使用数据库时.建议将oracle关闭,使其释放占用的资源. orac ...

  9. [SCOI2007]修车 BZOJ1070

    很久之前写的题了,今天翻出来写一篇博客复习一下... 分析: 考虑,T <= 1000,并不能针对这一维处理,所以考虑将,每个人拆点,之后,拆完之后表示,这个人第n-j+1个修k这辆车,也就是, ...

  10. Swift使用AlamoFire超时设置和事件处理

    一直在写swift项目,正好碰到服务器部署,请求超时或者请求失败的问题,页面就卡着不动了.顺手解决一下吧 差了些资料,说要设置超时时间 方法一: static let sharedSessionMan ...