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


    OJ链接:https://www.patest.cn/contests/pat-a-practise/1119

    我的分析过程:pat1119分析.pdf

     参考博客:https://www.cnblogs.com/xiongmao-cpp/p/6498672.html


    实现代码:

    //caution: should using initialize code: " t=0; "
    void setIn(int preS,int preE,int postS,int postE){
        if(preS==preE){
            in[t++]=pre[preS];
            return;
        }
        //finding the elem which is the root of left sub_tree
        int i=postS;
        while(i<=postE && post[i]!=pre[preS+1]) i++;
        //calculate the numbers of left sub_tree
        int leftNum=i-postS+1;
        //is paradox
        if(i==postE-1){
            flag=0;
            setIn(preS+1,preS+leftNum,postS,i);//default consider this is a right leaf
            in[t++]=pre[preS];
            return;
        }
        //build the in_order traversal sequence
        setIn(preS+1,preS+leftNum,postS,i);
        in[t++]=pre[preS];
        setIn(preS+leftNum+1,preE,i+1,postE-1);
    }

    完整代码:

    #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 10000
    #define MAX 0x06FFFFFF
    #define V vector<int>
    
    using namespace std;
    
    int in[LEN];
    int pre[LEN];
    int post[LEN];
    int t=0;
    int flag=1;
    
    //caution: should using initialize code: " t=0; "
    void setIn(int preS,int preE,int postS,int postE){
        if(preS==preE){
            in[t++]=pre[preS];
            return;
        }
        //finding the elem which is the root of left sub_tree
        int i=postS;
        while(i<=postE && post[i]!=pre[preS+1]) i++;
        //calculate the numbers of left sub_tree
        int leftNum=i-postS+1;
        //is paradox
        if(i==postE-1){
            flag=0;
            setIn(preS+1,preS+leftNum,postS,i);//default consider this is a right leaf
            in[t++]=pre[preS];
            return;
        }
        //build the in_order traversal sequence
        setIn(preS+1,preS+leftNum,postS,i);
        in[t++]=pre[preS];
        setIn(preS+leftNum+1,preE,i+1,postE-1);
    }
    
    int main(){
    //    freopen("d:/input/A1119_2.txt","r",stdin);
        int n,i;
        scanf("%d",&n);
        FF(i,n) scanf("%d",&pre[i]);
        FF(i,n) scanf("%d",&post[i]);
        setIn(0,n-1,0,n-1);
        puts(flag?"Yes":"No");
        FF(i,n){
            O("%d",in[i]);
            if(i!=n-1)O(" ");
        }
        puts("");
        return 0;
    }
    View Code
  • 相关阅读:
    延迟加载和缓存
    动态SQL
    Mybatis框架模糊查询+多条件查询
    mybatis增删改
    初始mybatis(二)
    Struts2框架和SpringMvc框架的区别
    MyBatis框架与Hibernate 框架的区别
    初始mybatis
    Servlet
    find命令使用
  • 原文地址:https://www.cnblogs.com/TQCAI/p/8404575.html
Copyright © 2020-2023  润新知