1020 Tree Traversals (25分)(一个字短!)

PAT-A 1020 Tree Traversals (25 分) 二叉树后中序遍历转按层遍历(C++题解) 1020 Tree Traversals (25 分) 题目传送门:1020 Tree Traversals (25 分) 一、题目大意 给出二叉树的后序遍历和中序遍历,求二叉树的按层遍历。 二、解题思路 要想知道按层遍历的结果,只需要在遍历二叉树的过程中将每个节点的值存到数组对应下标里就行了。通常我们对二叉树的标号都是根节点的下标为1。如果当前点的下标是i,则其左孩子的下标是2∗i2*i2∗i... 阅读详情

1020 Tree Traversals (25分)

分析

给出二叉树的后序遍历序列和中序遍历序列,可唯一确定二叉树,从而可输出层序遍历序列

代码

#include<iostream>
#include<queue>
using namespace std;
int n, post_odr[33], in_odr[33], level_odr[33], cnt = 0;
typedef pair<pair<int, int>, pair<int, int> > node;
void getlevel(node p) {
 queue<node> q;
 q.push(p);
 while (!q.empty()) {
  p = q.front();
  q.pop();
  level_odr[cnt++] = post_odr[p.first.second];
  int i = p.second.first;
  for (; i <= p.second.second; ++i)
   if (in_odr[i] == post_odr[p.first.second])break;
  if (i > p.second.first)q.push(pair<pair<int, int>, pair<int, int> >(pair<int, int>(p.first.first, p.first.first + i - 1 - p.second.first), pair<int, int>(p.second.first, i - 1)));
  if (i < p.second.second)q.push(pair<pair<int, int>, pair<int, int> >(pair<int, int>(p.first.second + i - p.second.second, p.first.second - 1), pair<int, int>(i + 1, p.second.second)));
 }
}
int main() {
 scanf("%d", &n);
 for (int i = 0; i < n; ++i)scanf("%d", &post_odr[i]);
 for (int i = 0; i < n; ++i)scanf("%d", &in_odr[i]);
 node p = pair<pair<int, int>, pair<int, int> >(pair<int, int>(0, n - 1), pair<int, int>(0, n - 1));
 getlevel(p);
 for (int i = 0; i < n; ++i)printf("%d%s", level_odr[i], i == n - 1 ? "" : " ");
 return 0;
}
【AI大模型应用开发】教你用AI轻松将你的知识库变为知识图谱(附完整代码,直接运行) 本文我们来介绍如何使用AI,将自己的知识库自动转换为知识图谱,并进行可视化展示。 阅读详情

相关推荐

Unity安卓打包实战:SDK与JDK配置全攻略

本文详细介绍了Unity安卓打包过程中SDK与JDK的配置方法,包括工具下载、安装步骤、环境变量设置及常见错误排查。通过实战指南帮助开发者快速完成Unity安卓打包环境搭建,提升开发效率。

weixin_30338481的博客 417

1020 Tree Traversals (25)(25 分)

1020 Tree Traversals (25)(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 or...

galesaur_wcy 701

对E-R模型向关系模式转换的认识

一:基本E-R图的转换 1     属性处理 派生属性:直接忽略。 复合属性:展平技术。 多值属性:单独建立一个关系表,关系名就是多值属性名,如果是实体集的多值属性,关系的属性就是实体集的码加上多值属性,如果是联系集的多值属性,关系的属性就是各个实体集的码加上多值属性,关系的码要具体情况具体分析。 2     基本E-R图转换为关系模式 强实体集:一个关系,关系名就是强实体集名,关系属

优快云_LYY的专栏 2734

1020 Tree Traversals (25分)

020Tree 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 tr...

Yanhaoming1999的博客 270

自动驾驶轨迹规划算法——Apollo Lattice Planner

Apollo Lattice Planner是百度Apollo自动驾驶系统中的一种基于采样的实时轨迹规划算法。其核心思想是在离散化的状态空间中,对终端状态进行采样,并通过多项式曲线连接当前状态与候选终端状态,形成候选轨迹集,最后基于代价函数选择最优轨迹。该算法将车辆可达状态按纵向距离、横向偏移和速度等维度规则离散化,形成"格点"状态空间。 Lattice Planner通过三个主要步骤实现轨迹规划:1)终端状态采样,在规划时域内枚举多种驾驶意图;2)轨迹生成,使用五次多项式连接起点与各终

swaggytiger的博客 1624

2026 年 3 月青少年软编等考 C/C++ 三级测试题解析

摘要 本文包含三部分内容: 单选题(10题):考察C++基础语法和数据结构知识,涉及vector、栈、队列的操作和特性。 判断题(10题):测试对动态数组、栈、队列、字符串操作的理解,包括内存管理、访问方式和函数返回值等。 编程题(1题):模拟火车站售票窗口排队场景,要求根据输入输出各窗口的排队情况,重点考查队列的实际应用和边界条件处理。 答案解析详细,涵盖常见编程考点,适合检验基础知识的掌握程度。

发布少儿编程考级的真题和优秀竞赛题解析,以及一些讲义。 253

1020 Tree Traversals (25 分)

1020Tree 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 ...

qq_32691667的博客 453

【数据集处理】基于ERA5 气温/湿度数据计算湿球温度并判别湿热浪

【数据集处理】基于ERA5 气温/湿度数据计算湿球温度并判别湿热浪

WW、forever的博客 488

1020 Tree Traversals (25 分) 题解

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 ...

lzy我就来随便逛逛的博客 591

Unity 编辑器篇|(一)MenuItem菜单栏

"一级菜单/二级菜单上/三级菜单""一级菜单/二级菜单中 %#_B""一级菜单/二级菜单下 %&_C""一级菜单/二级菜单中 %#_B""一级菜单/二级菜单下 %&_C""GameObject/二级菜单""CONTEXT/组件名/菜单名""一级菜单/二级菜单上 #_A""Assets/二级菜单""一级菜单/二级菜单下""一级菜单/二级菜单上""一级菜单/二级菜单中""一级菜单/二级菜单下""一级菜单/二级菜单下""一级菜单/二级菜单上""一级菜单/二级菜单中""一级菜单/二级菜单下""自定义/Test"

游戏开发小Y的博客 13万+

【图像超分】论文复现:轻量化超分 | 残差特征蒸馏网络RFDN的Pytorch源码复现,跑通源码,整合到EDSR-PyTorch中进行训练,补充CCA的实现!

本文介绍了将RFDN(残差特征蒸馏网络)整合到EDSR-PyTorch框架中的实现过程。主要内容包括:1)环境配置与数据集准备;2)测试源码提供的x4预训练模型,并通过修改图像裁剪函数解决HR与SR尺寸不一致问题;3)训练过程详解,包括将RFDN源码模块整合到EDSR框架中,调整模型参数输入方式。文章还提供了完整的代码实现、训练好的模型权重下载链接,并对RFDN架构进行了详细拆解与注释。通过将RFDN与EDSR相结合,实现了轻量级图像超分辨率重建,获得了PSNR/SSIM等指标及可视化结果。

主要更新底层视觉(去噪、超分等)相关的科研内容,形式为【论文精读】+【论文复现】 433

1020 Tree Traversals (25 分)

1020Tree 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 ...

weixin_38902950的博客 254

FP16、BF16、INT8、INT4精度模型加载所需显存以及硬件适配的分析

本文主要介绍了FP16、INT8、INT4精度模型加载占用显存大小的分析,希望对学习大语言模型的同学们有所帮助。 文章目录 1. 前言 2. 模型加载显存占用大小 3. 不同精度是否与硬件适配

herosunly的博客 23万+

Day45: [PAT甲级] 1020 Tree Traversals (25分)

Day45: [PAT甲级] 1020 Tree Traversals (25分) 题源: 来自PAT甲级题库: https://pintia.cn/problem-sets/994805342720868352/problems/994805485033603072 代码: dirty code凑合看吧 思路就是通过前序遍历和中序遍历直接把树构造好,然后层序遍历。 #include<io...

qq2215459786的博客 159

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

1020 Tree Traversals (25 分)

1020Tree 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 ...

weixin_42105789的博客 620

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

PAT甲级 - 1020 Tree Traversals (25 分)

题目链接:PAT甲级 - 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...

215

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

【1020】Tree Traversals (25 分)

#include<iostream> #include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> #include<algorithm> #include<map> #include<vector> #inclu...

发现问题,并解决问题,批判性思维 564

PAT 1020 Tree Traversals (25 分)

1020Tree 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 ...

~ 513
上一篇: 1010 Radix (25分)(一个字短!)
下一篇: 1024 Palindromic Number (25分)(一个字短!)
大了七八个
博客等级 码龄8年 2粉丝 30原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值