后續遍歷的實現:
數據結構樹中的后續遍歷,這里提供簡單實例,代碼中有注釋,大家參考下!
看下實現效果:
題目及分析
給定樹的先序遍歷和中序遍歷,求后續遍歷
輸入
abdec
dbeac
輸出
debca
三、實現代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#include <iostream> #include <string> using namespace std; string s1= "abdec" ; //先序遍歷 string s2= "dbeac" ; //中序遍歷 void calc( int l1, int r1, int l2, int r2){ int m=s2.find(s1[l1]); //cout<<m<<endl; if (m>l2) calc(l1+ 1 ,l1+m-l2,l2,m- 1 ); if (m<r2) calc(l1+m-l2+ 1 ,r1,m+ 1 ,r2); //cout<<s2[m]<<" "; cout<<s1[l1]; } int main(){ calc( 0 ,s1.length()- 1 , 0 ,s2.length()- 1 ); cout<<endl; return 0 ; } |
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://www.cnblogs.com/Renyi-Fan/p/7220299.html