zoukankan      html  css  js  c++  java
  • 二叉树 | 根据前序、后序生成中序

    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
  • 相关阅读:
    ThreadSafety with the AutoResetEvent, ManualResetEvent Class(Synchronization of .net)
    使用Python SMTP发送邮件
    flask项目中设置logo
    如何解决Bootstrap中分页不能居中的问题
    pip install mysql_python报错解决办法
    git上拉项目
    AttributeError: 'str' object has no attribute 'decode'
    pycharm设置SDK
    为git创建远程仓库
    开发过程中git的使用
  • 原文地址:https://www.cnblogs.com/TQCAI/p/8404575.html
Copyright © 2011-2022 走看看