当前位置:网站首页>L2-006 树的遍历 (25 分)
L2-006 树的遍历 (25 分)
2022-07-06 09:14:00 【%xiao Q】
题目
给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列。这里假设键值都是互不相等的正整数。
输入格式:
输入第一行给出一个正整数N(≤30),是二叉树中结点的个数。第二行给出其后序遍历序列。第三行给出其中序遍历序列。数字间以空格分隔。
输出格式:
在一行中输出该树的层序遍历的序列。数字间以1个空格分隔,行首尾不得有多余空格。
输入样例:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
输出样例:
4 1 6 3 5 7 2
分析
这题我们得知道如何在已知二叉树的中序遍历和后序遍历的情况下建树,这里我们可以这样做,后续遍历的最后一个节点一定是根节点,然后在中序遍历中找该节点,根据中序遍历的特性可以分开左右子树,然后在递归左子树和有子树,进行同样的操作。
然后就是程序遍历,这里直接用bfs搜即可。
参考代码:
#include <iostream>
#include <cstdio>
#include <set>
#include <vector>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
#include <unordered_map>
#define LL long long
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define reps(i, a, b) for(int i = a; i < b; i++)
#define pre(i, a, b) for(int i = b; i >= a; i--)
using namespace std;
const int N = 10010;
int pos, n;
int pre[N], in[N], post[N];
struct node
{
int l, r, w;
}T[N];
// 建树
void creat(int inl, int inr, int u)
{
if(inl > inr) return ;
// puts("hh");
// cout << u << endl;
T[u].w = post[pos--];
T[u].l = 2 * u, T[u].r = 2 * u + 1;
int mid;
for(mid = inl; mid <= inr; mid++)
if(T[u].w == in[mid]) break;
//必须先递归右子树,因为后续遍历的根节点前面一些点一定是右子树的点,
// 而我们是根据后续遍历的根节逐渐前移来去找递归的
creat(mid + 1, inr, 2 * u + 1);
creat(inl, mid - 1, 2 * u);
}
// 进行层序遍历
void bfs()
{
int idx = 0;
queue<int> q;
q.push(1);
while(q.size())
{
node t = T[q.front()];
q.pop();
if(!idx) cout << t.w;
else cout << " " << t.w;
idx++;
if(T[t.l].w != -1) q.push(t.l);
if(T[t.r].w != -1) q.push(t.r);
}
}
int main()
{
cin >> n;
rep(i, 1, n) cin >> post[i];
rep(i, 1, n) cin >> in[i];
memset(T, -1, sizeof T);
pos = n;
creat(1, n, 1);
bfs();
cout << endl;
return 0;
}
边栏推荐
- Remember the interview algorithm of a company: find the number of times a number appears in an ordered array
- Windows cannot start the MySQL service (located on the local computer) error 1067 the process terminated unexpectedly
- PyCharm中无法调用numpy,报错ModuleNotFoundError: No module named ‘numpy‘
- 虚拟机Ping通主机,主机Ping不通虚拟机
- QT creator support platform
- Leetcode 461 Hamming distance
- QT creator runs the Valgrind tool on external applications
- Django运行报错:Error loading MySQLdb module解决方法
- Esp8266 at+cipstart= "", "", 8080 error closed ultimate solution
- Error reporting solution - io UnsupportedOperation: can‘t do nonzero end-relative seeks
猜你喜欢
Esp8266 at+cipstart= "", "", 8080 error closed ultimate solution
Dotnet replaces asp Net core's underlying communication is the IPC Library of named pipes
基于apache-jena的知识问答
【博主推荐】C#生成好看的二维码(附源码)
Swagger、Yapi接口管理服务_SE
La table d'exportation Navicat génère un fichier PDM
打开浏览器的同时会在主页外同时打开芒果TV,抖音等网站
Use dapr to shorten software development cycle and improve production efficiency
机器学习--人口普查数据分析
[number theory] divisor
随机推荐
csdn-Markdown编辑器
数据库高级学习笔记--SQL语句
Record a problem of raspberry pie DNS resolution failure
[number theory] divisor
Summary of numpy installation problems
Other new features of mysql18-mysql8
JDBC principle
【博主推荐】asp.net WebService 后台数据API JSON(附源码)
安全测试涉及的测试对象
Invalid global search in idea/pychar, etc. (win10)
How to configure flymcu (STM32 serial port download software) is shown in super detail
QT creator create button
Windows cannot start the MySQL service (located on the local computer) error 1067 the process terminated unexpectedly
软件测试与质量学习笔记3--白盒测试
[recommended by bloggers] C MVC list realizes the function of adding, deleting, modifying, checking, importing and exporting curves (with source code)
Django running error: error loading mysqldb module solution
项目实战-后台员工信息管理(增删改查登录与退出)
QT creator custom build process
【博主推荐】SSM框架的后台管理系统(附源码)
解决安装Failed building wheel for pillow