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

这题的核心

  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. 通过yum源在centOS7安装mysql8

    1.去官网下载rpm文件,该文件专门用于yum安装方式: 到官网https://www.mysql.com/downloads/下载社区版Community(针对个人),如下图: 然后拉到最下面,我下 ...

  2. SDN期末作业验收

    作业链接:https://edu.cnblogs.com/campus/fzu/SoftwareDefinedNetworking2017/homework/1585 负载均衡程序 1.github链 ...

  3. django复习-3-请求与响应

    一.请求request 前端向后端传递参数有几种方式? 提取URL的特定部分,如/weather/beijing/2018,可以在服务器端的路由中用正则表达式截取: "http://127. ...

  4. 数据结构习题Pop Sequence的理解----小白笔记^_^

    Pop Sequence(25 分) Given a stack which can keep M numbers at most. Push N numbers in the order of 1, ...

  5. 【js】实现继承的6种方法

    1.原型链 基本思想:利用原型链让一个引用类型继承另一个引用类型的属性和方法. 让原型对象(B.prototype)等于另一个类型的实例(new A()), 即B.prototype = new A( ...

  6. Python3 下找不到urllib2的问题

    Python 3.* 用urllib.request来代替原来的urllib2,因此调用的时候改为: >>> import urllib.request >>> u ...

  7. Spring framework3.2整合hibernate4.1报错:No Session found for current thread

    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransact ...

  8. VS2010自行编译OpenCV2.4.4时缺少python27_d.lib的解决方法

    错误 24 error LNK1104: 无法打开文件“python27_d.lib”  C:\OpenCV\VS2013_64\modules\python\LINK opencv_python 编 ...

  9. 谷歌开源漏洞跟踪工具 Monorail 存在跨站点搜索漏洞

    一名安全研究员表示,在谷歌开源漏洞跟踪工具 Monorail 中找到一个漏洞,可被用于执行跨站点搜索 (XS-Search) 攻击. Monorail 用于检查和 Chromium 相关项目中的问题, ...

  10. Python2.7-time

    time模块,与datetime模块功能有所重合,time较为简单明了,若只需要当前时间或日期或是sleep,直接用time模块,需要更复杂的时间间隔等情况用datetime模块更好 1.获得time ...