求后序遍历(信息学奥赛一本通-T1339)
生活随笔
收集整理的這篇文章主要介紹了
求后序遍历(信息学奥赛一本通-T1339)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
【題目描述】
輸入一棵二叉樹的先序和中序遍歷序列,輸出其后序遍歷序列。
【輸入】
共兩行,第一行一個字符串,表示樹的先序遍歷,第二行一個字符串,表示樹的中序遍歷。樹的結點一律用小寫字母表示。
【輸出】
一行,表示樹的后序遍歷序列。
【輸入樣例】
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; }?
總結
以上是生活随笔為你收集整理的求后序遍历(信息学奥赛一本通-T1339)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最小新整数(信息学奥赛一本通-T1231
- 下一篇: 书架(信息学奥赛一本通-T1228)