常见题了,分治思想,有一个结论划分后,将序列划分为更小的子集,继续应用该结论。

图简单,直接递归了,之前看过非递归的写法。。。忘了

Impl:

 1 #include <string>
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 void postOrder(string strPre, string strIn) {
 7     if (strPre.size() == 0 || strIn.size() == 0) return;
 8     char root = strPre[0];
 9     int index = strIn.find(root);
10 
11     string strPreL = strPre.substr(1, index);
12     string strInL = strIn.substr(0, index);
13     string strPreR = strPre.substr(index+1, strPre.length() - index - 1);
14     string strInR = strIn.substr(index + 1, strIn.length() - index - 1);
15 
16     postOrder(strPreL, strInL);
17     postOrder(strPreR, strInR);
18 
19     cout << root;
20 }
21 
22 
23 int main()
24 {
25     string strPre,strIn;
26     cin >> strPre >> strIn;
27     postOrder(strPre, strIn);
28     cout << endl;
29     
30     return 0;
31 }
View Code