已知先序遍历和中序遍历,输出他的后序遍历序列.

样例输入:

DBACEGF  ABCDEFG 输出:ACBFGED

BCAD CBAD 输出:CDAB

 1 #include "stdafx.h"
 2 #include<iostream>
 3 #include<string>
 4 using namespace std;
 5 void show(int n,string lhs,string rhs);
 6 int _tmain(int argc, _TCHAR* argv[])
 7 {
 8 string s1 = "DBACEGF",s2="ABCDEFG";
 9 int n=s1.size();
10 show(n,s1,s2);
11 return 0;
12 }
13 
14 void show(int n,string lhs,string rhs)
15 {
16     if(n<=0)
17     {
18         return;
19     }
20     char firstWords=lhs[0];
21     int n1=rhs.find(firstWords);//找到根节点在终须遍历中的位置.
22     int n2=n-n1-1;
23     show(n1,(lhs.substr(1)).substr(0,n1),rhs.substr(0,n1));//递归构造左子树的后序遍历
24     show(n2,lhs.substr(n1+1),rhs.substr(n1+1)); //递归构造右子树的后序遍历
25     cout<<firstWords;//输出根节点.
26 
27 }

 

转载于:https://www.cnblogs.com/crazycodehzp/p/3520422.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值