码迷,mamicode.com
首页 > 其他好文 > 详细

hihocoder(1049) 后序遍历

时间:2015-04-13 12:26:41      阅读:255      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!