首页 > 其他 > 详细

hihocoder(1049) 后序遍历

时间:2015-04-13 12:25:39      阅读:353      评论:0      收藏:0      [点我收藏+]

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

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

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

 

hihocoder(1049) 后序遍历

原文:http://www.cnblogs.com/sheepsheep/p/4421720.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!