当前位置:网站首页>二叉树遍历(DAY 74)
二叉树遍历(DAY 74)
2022-07-22 23:33:00 【张学恒】
1:题目
假定一棵二叉树的每个结点都用一个大写字母描述。
给定这棵二叉树的前序遍历和中序遍历,求其后序遍历。
输入格式
输入包含多组测试数据。
每组数据占两行,每行包含一个大写字母构成的字符串,第一行表示二叉树的前序遍历,第二行表示二叉树的中序遍历。
输出格式
每组数据输出一行,一个字符串,表示二叉树的后序遍历。
数据范围
输入字符串的长度均不超过 26。
输入样例:
ABC
BAC
FDXEAG
XDEFAG
输出样例:
BCA
XEDGAF
2:代码实现
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
void dfs(string pre, string in)
{
if (pre.empty()) return;
char root = pre[0];
int k = in.find(root);
dfs(pre.substr(1, k), in.substr(0, k));
dfs(pre.substr(k + 1), in.substr(k + 1));
cout << root;
}
int main()
{
string pre, in;
while (cin >> pre >> in)
{
dfs(pre, in);
cout << endl;
}
return 0;
}
边栏推荐
猜你喜欢

数据分析与隐私安全成 Web3.0 成败关键因素,企业如何布局?

你知道怎么做好接口测试?

On the stability of common sorting

Qgraicsview implementation palette

聊聊队列(FIFO)的应用
![[arxiv2022] grouptransnet: Group transformer Network for RGB - D Salient Object Detection](/img/be/534f92578f0d825f9b565c1785af18.png)
[arxiv2022] grouptransnet: Group transformer Network for RGB - D Salient Object Detection

SOLIDWORKS CAM数据无法恢复,因为已检测到经化零件。

C语言编写“Hello World”挑战赛,你会如何作答?

小迪和小辉

XMODEM, ymodem and zmodem protocols are the three most commonly used communication protocols
随机推荐
【arXiv2022】GroupTransNet: Group Transformer Network for RGB-D Salient Object Detection
【Unity项目实践】FSM有限状态机
小迪和小辉
黑马程序员-接口测试-四天学习接口测试-第二天-接口用例设计,测试点,功能测试,安全测试,性能测试,单接口测试,业务场景测试用例,postman简介,安装
标准C语言10
Layout drawing of semi customized digital inverter
你必须知道的十大漏洞之失效的访问控制
手机键盘(DAY 73)
jmeter面试话术
08 爬虫项目
Shell变量、系统预定义变量$HOME、$PWD、$SHELL、$USER、自定义变量、特殊变量$n、$#、$*、[email protected]、$?、env看所有的全局变量值、set看所有变量
半定制数字反相器版图绘制
【OPENVX】对象基本使用之vx_node
Dark horse programmer - interface testing - four-day learning interface testing - the second day - Interface use case design, test points, function testing, security testing, performance testing, sing
Send benefits again! Calendar applet source code
Talking about -- network security architecture design (4)
SSH 免密登陆配置
Async and await are used in C to realize asynchronous UDP communication
JMeter distributed pressure measurement
Go gin: multi file upload