二叉树 | 根据前序、中序生成后序,根据后序、中序生成前序

使用的全局变量:

int in[LEN]={4,2,5,1,6,3,7};
int pre[LEN]={1,2,4,5,3,6,7};
int post[LEN];
int t=0;

根据前序、中序生成后序:

//caution: should using initialize code: " t=0; "
//pre in -> post
//        pre_start pre_end in_start in_end
void setPost(int ps,int pe,int is,int ie){
    if(ps>pe)return;//null
    if(ps==pe){
        post[t++]=pre[ps];
    }else{
        //find the elem is the pair of preOrder (ps)
        int i=is;
        while(in[i]!=pre[ps] && i<ie) i++;//redirect
        //left
        setPost(ps+1, ps+i-is, is, i-1);
        //right
        setPost(ps+i-is+1, pe, i+1, ie);
        //root
        post[t++]=pre[ps];
    }
}

根据后序、中序生成前序:

//caution: should using initialize code: " t=0; "
//post in -> pre
//        post_start post_end in_start in_end
void setPre(int ps,int pe,int is,int ie){
    if(ps>pe)return;//null
    if(ps==pe){
        pre[t++]=post[ps];
    }else{
        //find the elem is the pair of preOrder (ps)
        int i=is;
        while(in[i]!=post[pe] && i<ie) i++;//redirect
        //root
        pre[t++]=post[pe];
        //left
        setPre(ps, ps+i-is-1, is, i-1);
        //right
        setPre(ps+i-is, pe-1, i+1, ie);
    }
}

完整代码:

#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <map>


#define I scanf
#define OL puts
#define O printf
#define F(a,b,c) for(a=b;a<c;a++)
#define FF(a,b) for(a=0;a<b;a++)
#define FG(a,b) for(a=b-1;a>=0;a--)
#define LEN 7
#define MAX 0x06FFFFFF
#define V vector<int>

using namespace std;

int in[LEN]={4,2,5,1,6,3,7};
int pre[LEN]={1,2,4,5,3,6,7};
int post[LEN];
int t=0;

//caution: should using initialize code: " t=0; "
//pre in -> post
//        pre_start pre_end in_start in_end
void setPost(int ps,int pe,int is,int ie){
    if(ps>pe)return;//null
    if(ps==pe){
        post[t++]=pre[ps];
    }else{
        //find the elem is the pair of preOrder (ps)
        int i=is;
        while(in[i]!=pre[ps] && i<ie) i++;//redirect
        //left
        setPost(ps+1, ps+i-is, is, i-1);
        //right
        setPost(ps+i-is+1, pe, i+1, ie);
        //root
        post[t++]=pre[ps];
    }
}

//caution: should using initialize code: " t=0; "
//post in -> pre
//        post_start post_end in_start in_end
void setPre(int ps,int pe,int is,int ie){
    if(ps>pe)return;//null
    if(ps==pe){
        pre[t++]=post[ps];
    }else{
        //find the elem is the pair of preOrder (ps)
        int i=is;
        while(in[i]!=post[pe] && i<ie) i++;//redirect
        //root
        pre[t++]=post[pe];
        //left
        setPre(ps, ps+i-is-1, is, i-1);
        //right
        setPre(ps+i-is, pe-1, i+1, ie);
    }
}

int main(){
//    freopen("d:/input/A1128.txt","r",stdin);
    t=0;
    setPost(0,LEN-1,0,LEN-1);
    FF(t,LEN) O("%d ",post[t]);
    puts("");
    t=0;
    setPre(0,LEN-1,0,LEN-1);
    FF(t,LEN) O("%d ",pre[t]);
    return 0;
}
View Code

测试的数据结构:

  

:测试效果

原文地址:https://www.cnblogs.com/TQCAI/p/8401355.html