当前位置:网站首页>二叉树专题--P1030 [NOIP2001 普及组] 求先序排列
二叉树专题--P1030 [NOIP2001 普及组] 求先序排列
2022-07-02 07:21:00 【Morgannr】
[NOIP2001 普及组] 求先序排列
题目描述
给出一棵二叉树的中序与后序排列。求出它的先序排列。(约定树结点用不同的大写字母表示,长度$ \le 8$)。
输入格式
2 2 2行,均为大写字母组成的字符串,表示一棵二叉树的中序与后序排列。
输出格式
1 1 1行,表示一棵二叉树的先序。
样例 #1
样例输入 #1
BADC
BDCA
样例输出 #1
ABCD
提示
【题目来源】
NOIP 2001 普及组第三题
#define _CRT_SECURE_NO_WARNINGS 1
#include <bits/stdc++.h>
using namespace std;
//#define int long long
//#define map unordered_map
const int N = 10;
map<char, int> l, r, d;
char in[N], post[N];
int dfs1(int il, int ir, int pl, int pr)
{
if (pl > pr) return -1;
char root = post[pr];
int k = d[root];
l[root] = dfs1(il, k - 1, pl, k - 1 - il + pl);
r[root] = dfs1(k + 1, ir, k - il + pl, pr - 1);
return k;
}
void dfs2(int u)
{
if (u == -1) return;
cout << in[u];
if (l[in[u]] != -1) dfs2(l[in[u]]);
if (r[in[u]] != -1) dfs2(r[in[u]]);
}
signed main()
{
int T = 1; //cin >> T;
while (T--)
{
cin >> in >> post;
for (int i = 0; in[i]; ++i)
{
d[in[i]] = i;
}
int n = strlen(in);
int root = dfs1(0, n - 1, 0, n - 1);
dfs2(root);
puts("");
}
return 0;
}
边栏推荐
- 2022-06-17
- Ks009 implement pet management system based on SSH
- Flink submitter
- From Read and save in bag file Jpg pictures and PCD point cloud
- Start class, data analysis, high salary training plan, elite class
- 华为联机对战服务玩家掉线重连案例总结
- 【ARK UI】HarmonyOS ETS的启动页的实现
- Is this code PHP MySQL redundant?
- JSP webshell free -- webshell free
- SQOOP 1.4.6 INSTALL
猜你喜欢

Hdu1234 door opener and door closer (water question)

【AGC】如何解决事件分析数据本地和AGC面板中显示不一致的问题?
![[TS] 1368 seconds understand typescript generic tool types!](/img/2b/58a850b52ce8a9b2e6e7b6b72b0fe5.jpg)
[TS] 1368 seconds understand typescript generic tool types!

SPSS做Shapiro-Wilk正态分析

JSP webshell free -- webshell free

MYSQL环境配置

LeetCode+ 76 - 80 暴搜专题

JSP webshell免殺——JSP的基礎
![[visual studio] visual studio 2019 community version cmake development environment installation (download | install relevant components | create compilation execution project | error handling)](/img/9f/4265f1e3927fcf66602f0fc9e7a9d9.jpg)
[visual studio] visual studio 2019 community version cmake development environment installation (download | install relevant components | create compilation execution project | error handling)

js数组常用方法
随机推荐
12. Process synchronization and semaphore
SQOOP 1.4.6 INSTALL
2.hacking-lab脚本关[详细writeup]
互联网快讯:腾讯会议应用市场正式上线;Soul赴港递交上市申请书
2022爱分析· 国央企数字化厂商全景报告
UVM——Callback
The nanny level tutorial of flutter environment configuration makes the doctor green to the end
What are the popular frameworks for swoole in 2022?
PCL 投影点云
Internet News: Tencent conference application market was officially launched; Soul went to Hong Kong to submit the listing application
MySQL数据库远程访问权限设置
Set the playback speed during the playback of UOB equipment
华为AppLinking中统一链接的创建和使用
Shutter - canvas custom graph
SPSS做Shapiro-Wilk正态分析
Mysql database remote access permission settings
"Matching" is true love, a new attitude for young people to make friends
转换YV12到RGB565图像转换,附YUV转RGB测试
Is this code PHP MySQL redundant?
最详细MySql安装教程