求后序遍历(信息学奥赛一本通-T1339)
生活随笔
收集整理的這篇文章主要介紹了
求后序遍历(信息学奥赛一本通-T1339)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
【題目描述】
輸入一棵二叉樹(shù)的先序和中序遍歷序列,輸出其后序遍歷序列。
【輸入】
共兩行,第一行一個(gè)字符串,表示樹(shù)的先序遍歷,第二行一個(gè)字符串,表示樹(shù)的中序遍歷。樹(shù)的結(jié)點(diǎn)一律用小寫(xiě)字母表示。
【輸出】
一行,表示樹(shù)的后序遍歷序列。
【輸入樣例】
abdec
dbeac
【輸出樣例】
debca
【源程序】
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<string> #include<cstdlib> #include<queue> #include<vector> #define INF 0x3f3f3f3f #define PI acos(-1.0) #define N 101 #define MOD 123 #define E 1e-6 using namespace std; string str1,str2; void calculate(int left1,int right1,int left2,int right2) {int m=str2.find(str1[left1]);if(m>left2)calculate(left1+1,left1+m-left2,left2,m-1);if(m<right2)calculate(left1+m-left2+1,right1,m+1,right2);cout<<str1[left1]; } int main() {cin>>str1>>str2;calculate(0,str1.length()-1,0,str2.length()-1);cout<<endl;return 0; }?
總結(jié)
以上是生活随笔為你收集整理的求后序遍历(信息学奥赛一本通-T1339)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 最小新整数(信息学奥赛一本通-T1231
- 下一篇: 书架(信息学奥赛一本通-T1228)