当前位置:网站首页>二叉树专题--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;
}
边栏推荐
- Shapiro Wilk normal analysis by SPSS
- The URL in the RTSP setup header of the axis device cannot take a parameter
- Point cloud projection picture
- LabVIEW为什么浮点数会丢失精度
- JSP webshell免杀——webshell免杀
- Mongodb quickly get started with some simple operations of mongodb command line
- LeetCode+ 76 - 80 暴搜专题
- JSP webshell免殺——JSP的基礎
- Flink submitter
- Flink calculates topn hot list in real time
猜你喜欢

最详细MySql安装教程

Open the encrypted SQLite method with sqlcipher

Hdu1236 ranking (structure Sorting)

Retrofit's callback hell is really vulnerable in kotlin synergy mode!
![[SUCTF2018]followme](/img/63/9104f9c8bd24937b0fc65053efec96.png)
[SUCTF2018]followme

Session cookies and tokens

Kustomize使用手册

14. Code implementation of semaphore

Operator-1初识Operator

Common methods of JS array
随机推荐
Mongodb quickly get started with some simple operations of mongodb command line
快应用中实现自定义抽屉组件
The nanny level tutorial of flutter environment configuration makes the doctor green to the end
MYSQL环境配置
面对不确定性,供应链的作用
Open the encrypted SQLite method with sqlcipher
session-cookie与token
Operator-1初识Operator
华为联机对战服务玩家掉线重连案例总结
P1055 [noip2008 popularization group] ISBN number
Pywin32 opens the specified window
Sus system availability scale
JSP webshell free -- webshell free
Easyexcel, a concise, fast and memory saving excel processing tool
最详细MySql安装教程
Shutter - canvas custom graph
正则及常用公式
PCL之滤波
JSP webshell free -- the basis of JSP
《MySQL 8 DBA基础教程》简介