链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456


题目:

给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列。这里假设键值都是互不相等的正整数。

输入格式:

输入第一行给出一个正整数N(≤),是二叉树中结点的个数。第二行给出其后序遍历序列。第三行给出其中序遍历序列。数字间以空格分隔。

输出格式:

在一行中输出该树的层序遍历的序列。数字间以1个空格分隔,行首尾不得有多余空格。

输入样例:

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

输出样例:

4 1 6 3 5 7 2

思路:
模板题

代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue> using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int maxn=;
int n;
int mid[maxn],po[maxn]; struct node{
int l,r;
}T[maxn]; int mid_po_build(int la,int ra,int lb,int rb){
if(la>ra) return ;
int rt=po[rb];
int p1=la,p2;
while(mid[p1]!=rt) p1++;
p2=p1-la;
T[rt].l=mid_po_build(la,p1-,lb,lb+p2-);
T[rt].r=mid_po_build(p1+,ra,lb+p2,rb-);
return rt;
} void dfs(int rt){
queue<int>Q;
vector<int>v;
Q.push(rt);
while(!Q.empty()){
int w=Q.front();
Q.pop();
v.push_back(w);
if(T[w].l!=) Q.push(T[w].l);
if(T[w].r!=) Q.push(T[w].r);
}
int len=v.size();
for(int i=;i<len;i++){
printf("%d%c",v[i],i==(len-)?'\n':' ');
}
} int main(){
scanf("%d",&n);
for(int i=;i<n;i++) scanf("%d",&po[i]);
for(int i=;i<n;i++) scanf("%d",&mid[i]);
int rt=mid_po_build(,n-,,n-);
dfs(rt);
return ;
}

L2-006 树的遍历 (25 分)的更多相关文章

  1. PTA 7-3 树的遍历 (25分)

    PTA 7-3 树的遍历 (25分) 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(≤30),是二叉树中结点 ...

  2. PTA 03-树1 树的同构 (25分)

    题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/711 5-3 树的同构   (25分) 给定两棵树T1和T2.如果T1可以通过若干次左右 ...

  3. PTA 根据后序和中序遍历输出先序遍历 (25分)

    PTA 根据后序和中序遍历输出先序遍历 (25分) 本题要求根据给定的一棵二叉树的后序遍历和中序遍历结果,输出该树的先序遍历结果. 输入格式: 第一行给出正整数N(≤30),是树中结点的个数.随后两行 ...

  4. PTA 树的同构 (25分)

    PTA 树的同构 (25分) 输入格式: 输入给出2棵二叉树树的信息.对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N−1编号):随后N行,第i行对应编号第 ...

  5. 2020天梯赛总决赛L2-3 完全二叉树的层序遍历 (25分)

    题目:一个二叉树,如果每一个层的结点数都达到最大值,则这个二叉树就是完美二叉树.对于深度为 D 的,有 N 个结点的二叉树,若其结点对应于相同深度完美二叉树的层序遍历的前 N 个结点,这样的树就是完全 ...

  6. PAT 03-树1 树的同构 (25分)

    给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是"同构"的.例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A.B.G的左右孩子互换后 ...

  7. GPTL—练习集—006树的遍历

    #include<bits/stdc++.h> using namespace std; typedef int daTp;//datatype typedef struct BTNode ...

  8. L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...

  9. PAT A1102 Invert a Binary Tree (25 分)——静态树,层序遍历,先序遍历,后序遍历

    The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...

  10. PTA 根据后序和中序遍历输出先序遍历(25 分)

    7-1 根据后序和中序遍历输出先序遍历(25 分) 本题要求根据给定的一棵二叉树的后序遍历和中序遍历结果,输出该树的先序遍历结果. 输入格式: 第一行给出正整数N(≤30),是树中结点的个数.随后两行 ...

随机推荐

  1. BigDecimal比较大小,BigDecimal判断是否为0

    原文:https://blog.csdn.net/qq_34926773/article/details/83419004 BigDecimal类型的数据,需要比较大小:声明BigDescimal: ...

  2. An interesting combinational problem

    A question of details in the solution at the end of this post of the question is asked by me at MSE. ...

  3. VisualStudio 快捷键

    ctrl + o : 打开当前文件所在文件目录 ctrl + 鼠标左键 : 转到方法或者字段定义

  4. php框架之phalcon

    1.开发助手 1) 下载 git clone https://github.com/phalcon/cphalcon.git git clone https://github.com/phalcon/ ...

  5. php框架之thinkphp

    日常开发中经常使用thinkphp5进行开发工作,总结一些使用中遇到的问题和使用的东西 1. web内置服务 V5.1.5+版本开始,增加了启动内置服务器的指令,方便测试 >php think ...

  6. CentOS7切换源

    1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 2.下载新的CentOS-Base ...

  7. python-itchat 统计微信群、好友数量的实例

    今天小编就为大家分享一篇python-itchat 统计微信群.好友数量,及原始消息数据的实例,具有很好的参考价值,希望对大家有所帮助.一起跟随小编过来看看吧   效果:   好友: # 获取自己的用 ...

  8. ZooKeeper连接并创建节点以及实现分布式锁操作节点排序输出最小节点Demo

    class LockThread implements Runnable { private DistributedLock lock; public LockThread(int threadId, ...

  9. Git如何合并一个已经在GitHub上提交但没有合并的Pull Request请求

    步骤 进入Git仓库,执行curl -L https://github.com/<USER>/<REPO>/pull/<NO>.patch | git am

  10. 树莓派中QT实现串口通讯

    树莓派中QT实现串口通讯 开发平台为QT 此博客QT使用的为WiringPi驱动 我使用的串口调试助手为 cutecom 先简单说一些开发过程中需要注意的问题 Linux 下设备为 tty ,对应在 ...