当前位置:网站首页>根据前序中序求后序
根据前序中序求后序
2022-08-05 15:36:00 【-JMY-】
题目描述
给定一棵二叉树的前序遍历和中序遍历,求其后序遍历。
输入
读入2个两个字符串,每个一行,长度均小于等于26。
第一行为前序遍历,第二行为中序遍历。
二叉树中的结点名称以大写字母表示:A,B,C....。
输出
输出一行,为后序遍历的字符串。
样例输入
ABC CBA
样例输出
CBA
参考代码:
#include<bits/stdc++.h>
using namespace std;
string s1,s2;
int len;
int find(char x){
for(int i=0;i<=len;i++)
if(s2[i]==x)
return i;
}
void pre(int l1,int r1,int l2,int r2){
int ip=find(s1[l1]);
if(ip>l2)pre(l1+1,l1+ip-l2,l2,ip-1);
if(ip<r2)pre(ip-l2+l1+1,r1,ip+1,r2);
cout<<s2[ip];
return;
}
int main(){
cin>>s1>>s2;
len=s1.size()-1;
pre(0,len,0,len);
return 0;
}
边栏推荐
- 首创期货开户是否可靠?资金安全吗?
- BEVDet:High-Performance Multi-Camera 3D Object Detection in Bird-Eye-View 论文笔记
- 1704. 判断字符串的两半是否相似
- 学习笔记180—回归系数与相关系数的关系和区别
- 面试官:hold住了八股和算法,扫码登录应该怎么实现你总不会了吧
- 2264. Maximum 3 identical digits in a string
- PR Video Editing Tutorial - Video Effects and Transitions
- addSrouce(sourceFunction),但是我MysqlSource.build之后没有
- Study Notes 227—Word automatic catalog, there is a space after the catalog number, how can I set it to remove it?
- Read it all!Adapter technology in NLP
猜你喜欢

高数_证明_极限存在的单调有界准则

playwright录制脚本

语音聊天app开发——开发人员如何进行代码分析

盲盒+带货玩法,让你开“盲”盒

Locally boundedness of high number_proof_limit

Voice Chat App Development - How Developers Do Code Analysis

Live barrage

统计遗传学:第五章,多基因评分PGS

How to calculate heritability and breeding value in a full-sib family

Innovation Diffusion Cycle
随机推荐
你总说毕业遥遥无期,转眼就各奔东西
playwright recording script
playwright录制脚本
土耳其国防部:计划从乌克兰购粮的船只已通过安全检查
Statistical genetics: the fourth chapter, GWAS analysis
High number of _ _ limit local insurance number
How to calculate heritability and breeding value in a full-sib family
[kali-Metasploit] Armitage FAQ: sudo permission, unable to connect to database, service not found
学习笔记180—回归系数与相关系数的关系和区别
高数_证明_极限存在的夹逼准则
Redis - Talking about master-slave synchronization
Study Notes 227—Word automatic catalog, there is a space after the catalog number, how can I set it to remove it?
【云原生 · Kubernetes】部署博客系统
刷题《剑指Offer》day08
首创期货开户是否可靠?资金安全吗?
这个「令人上头」的赛道,俞敏洪、高瓴都入了,红杉和腾讯会来吗?
数据思维总结:
浅谈推荐系统中的样本拼接
请指教同花顺究竟怎么开户?在线开户安全么?
How to scientifically predict the height of children