#include <cstdio>
#include <iostream>
using namespace std;
#define N 100
void pre2post(char pre[] , int preL,int preR , char post[] , int postL , int postR)
{
if(preL <= preR)
{
post[postR] = pre[preL];
int halfpos = (preR - preL) / 2; // 数量对半分
pre2post(pre , preL+1 , preL + halfpos, post , postL ,postL + halfpos - 1); // 左
pre2post(pre , preL+halfpos+1 , preR, post , postL + halfpos ,postR-1); // 右
}
}
int main()
{
//freopen("in.txt","r",stdin);
/*
A
/ \
B C
/ \ / \
D E F G
*/
char pre[N] = "ABDECFG";
char post[N]; // D E B F G C A
pre2post(pre,0,6, post, 0 ,6);
for(int i = 0 ; i <= 6 ; i ++)
{
printf("%c ", post[i]);
}
return 0;
}
满二叉树根据前序求后序
最新推荐文章于 2022-05-22 14:33:31 发布