1020 Tree Traversals (25 分)
 

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

7
2 3 1 5 7 6 4
1 2 3 4 5 6 7

Sample Output:

4 1 6 3 5 7 2

这个推了蛮久的。。。。。不太熟练

    //左子树
if(k-l2==){
tree[root].l = -;//左子树为空
}
else{
buildTree(*root,l1,l1+k-l2-,l2,k-);
}
//右子树
if(k+>r2){
tree[root].r = -;//右子树为空
}
else{
buildTree(*root+,l1+k-l2,r1-,k+,r2);
}

AC代码:

#include<bits/stdc++.h>
using namespace std;
int post[];
int in[];
int n;
struct node{
int v;
int l;
int r;
}tree[];
queue<int>q;
void buildTree(int root,int l1,int r1,int l2,int r2){//后序,中序
//cout<<root<<" "<<l1<<"-"<<r1<<" "<<l2<<"-"<<r2<<endl;
//先找根节点
tree[root].v = post[r1];
if(l1==r1){ //只有一个节点
tree[root].l = -;
tree[root].r = -;
return;
}else{
tree[root].l = *root;
tree[root].r = *root+;
}
//找一下根在中序上的位置
int k;
for(int i=l2;i<=r2;i++){
if(in[i]==post[r1]){
k=i;//前面k-l2个数就是左子树
break;
}
}
//左子树
if(k-l2==){
tree[root].l = -;//左子树为空
}
else{
buildTree(*root,l1,l1+k-l2-,l2,k-);
}
//右子树
if(k+>r2){
tree[root].r = -;//右子树为空
}
else{
buildTree(*root+,l1+k-l2,r1-,k+,r2);
}
}
int main(){
int n;
cin>>n;
for(int i=;i<=n;i++){
cin>>post[i];
}
for(int i=;i<=n;i++){
cin>>in[i];
}
buildTree(,,n,,n);
//bfs 层序
while(!q.empty()) q.pop();
q.push();
while(!q.empty()){
node x=tree[q.front()];
q.pop();
cout<<x.v;
if(x.l!=-){
q.push(x.l);
}
if(x.r!=-){
q.push(x.r);
}
if(!q.empty()){
cout<<" ";
}
}
return ;
}

PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)的更多相关文章

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

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

  2. PAT Advanced 1020 Tree Traversals (25 分)

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

  3. PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]

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

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

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

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

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

  6. PAT 甲级 1020 Tree Traversals (二叉树遍历)

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

  7. PAT 甲级 1020 Tree Traversals

    https://pintia.cn/problem-sets/994805342720868352/problems/994805485033603072 Suppose that all the k ...

  8. 1020 Tree Traversals (25分)思路分析 + 满分代码

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

  9. 1020 Tree Traversals (25 分)

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

随机推荐

  1. Django drf:序列化增删改查、局部与全局钩子源码流程、认证源码分析、执行流程

    一.序列化类的增.删.改.查 用drf的序列化组件   -定义一个类继承class BookSerializer(serializers.Serializer):   -写字段,如果不指定source ...

  2. npm设置成淘宝镜像

    1.淘宝 npm 网址 https://npm.taobao.org/ 2.修改 2.1 通过命令配置 2.1.1. 命令 npm config set registry https://regist ...

  3. Spark1

    Spark集群 0.0体验安装Spark在集群单节点 1.tar tar -xzvf xxx.tgz -C /soft/ ln -s /soft/spark-2.1.0-bin-hadoop2.7 / ...

  4. AKS素性检测

    2002年印度数学家Manindra Agrawal, Neeraj Kayal,Nitin Saxena 给出了一个是否为素数的判别准则. 定理一:设 $a$ 是于 $p$ 互素的整数,则 $p$ ...

  5. 2019牛客多校D move——乱搞&&思维题

    题意 给定 $n$ 个物品,体积分别为 $v_i$,现有 $K$ 的容积一样的箱子,按如下策略装入物品:每次选取尽可能大的装入(较大的不能装入时可以向小的找),依次装入箱子. 分析 首先,不具有严格的 ...

  6. FFmpeg处理音视频流程学习笔记

    原文作者:一叶知秋0830 链接:https://www.jianshu.com/p/1b715966af50 FFmpeg处理音视频完整流程包括5个阶段(输入文件—>编码数据包—>解码后 ...

  7. P4178 Tree 点分治

    思路:点分治 提交:1次 题解: 要求权值和\(\leq K\) 的路径,我们可以类比点分治的模板,把长为\(len\)是否存在,改为\(len\)的路径的条数,并用用树状数组维护前缀和,这样就可以求 ...

  8. Activiti服务类- RuntimeService服务类

    一共89个接口1.启动流程实例(20个方法)//使用给定的键在流程定义的最新版本中启动一个新的流程实例.ProcessInstance startProcessInstanceByKey(String ...

  9. 数据库读写分离、分表分库——用Mycat

    转:     https://www.cnblogs.com/joylee/p/7513038.html 系统开发中,数据库是非常重要的一个点.除了程序的本身的优化,如:SQL语句优化.代码优化,数据 ...

  10. web文件夹上传

    需求:项目要支持大文件上传功能,经过讨论,初步将文件上传大小控制在500M内,因此自己需要在项目中进行文件上传部分的调整和配置,自己将大小都以501M来进行限制. 第一步: 前端修改 由于项目使用的是 ...