满二叉树根据前序求后序

#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;  
} 
    原文作者:满二叉树
    原文地址: https://blog.csdn.net/qq_26437925/article/details/48212119
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞