首页 > 其他 > 详细

Uva 536 Tree Recovery

时间:2014-10-31 22:04:45      阅读:264      评论:0      收藏:0      [点我收藏+]
 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 string pr,in;
 6 string postorder;
 7 void process(string preord,string inord);
 8 int main()
 9 {
10     //freopen("D:\\acm.txt","r",stdin);
11     while(cin>>pr>>in){
12         postorder.clear();
13         process(pr,in);
14         cout<<postorder<<endl;
15     }
16     return 0;
17 }
18 void process(string preord,string inord){
19     int Size = preord.size();
20     if(Size>0){
21         int p = inord.find(preord[0]);
22         process(preord.substr(1,p),inord.substr(0,p));//左树
23         process(preord.substr(p + 1,Size - (p + 1)),inord.substr(p + 1,Size - (p + 1)));//右树
24         postorder.push_back(preord[0]);
25     }
26 }


题目要求是根据先序、中序求后序。
核心部分是递归调用。(注意递归调用的顺序)

Uva 536 Tree Recovery

原文:http://www.cnblogs.com/ohxiaobai/p/4066027.html

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