Uva 536 Tree Recovery

本文介绍了如何通过递归方法,根据先序和中序遍历序列来求解后序遍历序列的问题。重点在于理解递归调用的顺序和逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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 }


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

转载于:https://www.cnblogs.com/ohxiaobai/p/4066027.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值