当前位置:网站首页>Recursively solve the traversal of binary trees (often test basic examples)
Recursively solve the traversal of binary trees (often test basic examples)
2022-07-18 13:32:00 【Wu Yu 4】
Suppose that each node of a binary tree is described by a capital letter .
Given the preorder traversal and inorder traversal of this binary tree , Find the postorder traversal .
Input format
Input contains multiple sets of test data .
Each group of data occupies two rows , Each line contains a string of uppercase letters , The first line represents the preorder traversal of the binary tree , The second line represents the middle order traversal of the binary tree .
Output format
Output one row per group of data , A string , Represents the post order traversal of a binary tree .
Data range
The length of the input string does not exceed 26.
Input sample mile :
ABC
BAC
FDXEAG
XDEFAG
sample output :
BCA
XEDGAFIntroduction premise :
The so-called tree Prefix expression , The order is “ Root left and right ”; Infix expression , The order is “ Left root right ”; Postfix expression , The order is “ Left and right ”.
We can know Give the preorder traversal 、 The middle order traversal can calculate the post order traversal .
Give the postorder traversal 、 The middle order traversal can also calculate the pre order traversal .
That is to say , You must traverse with middle order Together, we can push one by two .
For example :
The preorder traversal is :“FDXEAG”, The middle order traversal is :“XDEFAG”.

From this we can solve recursively
The code is :
#include<bits/stdc++.h>
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;// Give prefix 、 Infix expression
while(cin>>pre>>in)
{
dfs(pre,in);// Recursively find the suffix expression
cout<<endl;
}
return 0;
}边栏推荐
- 语言AI原来知道自己的回答是否正确!伯克利等高校新研究火了,网友:危险危险危险...
- Memory management page properties
- Summary of various parameters of ultra micro motherboard
- 模板_实数二分
- 130 多家企业,3500 多位开发者,共建 openGauss 开源数据库根社区
- 电脑如何恢复已删除文件 如何恢复被删除的数据
- TCP/IP之常用协议
- [TinyML]APQ:Joint Search for Network Architecture, Pruning and Quantization Policy
- [machine learning] integrated learning - ensemble learning
- Some points for attention in drawing excel charts
猜你喜欢

【软件测试】08 -- 黑盒测试方法(边界值分析法、因果图与决策表法)

剑指 Offer 48. 最长不含重复字符的子字符串

The static variable looks a little dizzy, and the GDB assembly is awake

OpenCV、EmguCV和OpenCvSharp指针访问图像像素值耗时测评(附源码)
![[programming compulsory training 9] the number of schemes in the grid (recursion) + alternative addition (bit operation)](/img/84/362c8fb54f2d3b0fe6fb7f915c0b46.png)
[programming compulsory training 9] the number of schemes in the grid (recursion) + alternative addition (bit operation)

剑指 Offer 04. 二维数组中的查找

Problems encountered in deploying edusoho on the Intranet

【机器学习】集成学习 - Ensemble Learning

快速上手Jupyter Notebook

社区峰会|Pulsar Summit 旧金山峰会议题亮点曝光!
随机推荐
5、 Basic composition & assertion of JMeter script
[programming training 4] calculate candy + hexadecimal conversion
[machine learning] PCA - principal component analysis
剑指 Offer 47. 礼物的最大价值
【机器学习】集成学习 - Ensemble Learning
Aomei easy clone system disk backup
ACL技术
u盘删除的文件怎么恢复
Language AI originally knew whether his answer was correct! New research in Berkeley and other universities is hot, netizen: dangerous, dangerous, dangerous
DzzOffice_flowplayer播放器更改
五、jmeter脚本的基本构成&断言
【机器学习 - 决策树】信息增益
[machine learning] random forest
剑指 Offer 48. 最长不含重复字符的子字符串
USB protocol (I)
电脑如何恢复已删除文件 如何恢复被删除的数据
[programming training 5] continuous maximum sum + statistical palindrome
ES6 note 2
六、Jmeter定时器
How to solve pycharm's inability to input Chinese: