当前位置:网站首页>二叉树专题--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;
}
边栏推荐
猜你喜欢

618再次霸榜的秘密何在?耐克最新财报给出答案

最详细MySql安装教程
![[SUCTF2018]followme](/img/63/9104f9c8bd24937b0fc65053efec96.png)
[SUCTF2018]followme
![[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)

HDU1228 A + B(map映射)

SUS系统可用性量表

1287_ Implementation analysis of prvtaskistasksuspended() interface in FreeRTOS

Disassembling Meitu SaaS: driving the plane to change the engine

2022-06-17

Operator-1 first acquaintance with operator
随机推荐
Mysql database remote access permission settings
In the face of uncertainty, the role of supply chain
【AGC】构建服务3-认证服务示例
nodejs+express+mysql简单博客搭建
Retrofit's callback hell is really vulnerable in kotlin synergy mode!
SQOOP 1.4.6 INSTALL
Is this code PHP MySQL redundant?
面对不确定性,供应链的作用
flume 190 INSTALL
JSP webshell免殺——JSP的基礎
Database dictionary Navicat automatic generation version
Flink calculates topn hot list in real time
2022-06-17
正则及常用公式
拆解美图SaaS:开着飞机换引擎
2022爱分析· 国央企数字化厂商全景报告
Record attributeerror: 'nonetype' object has no attribute 'nextcall‘
Transport Optimization abstraction
P1055 [NOIP2008 普及组] ISBN 号码
Rapid prototyping