zoukankan      html  css  js  c++  java
  • 【题解搬运】PAT_A1020 树的遍历

    题目

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

    题解

    这题搞了我一个小时!!!好气啊,我们好好梳理一下这条题目。其实摸鱼了半个小时

    给定一棵二叉树的后根遍历和中根遍历,求其前根遍历。
    如何做呢?这里一定要有递归的思想。我们设定一个 int makeTree(int l,int r); 函数返回的是这棵树的根在动态数组(vector)的位置(感觉指针更好写);l和r是它元素的范围。那么显然main函数调用的是 makeTree(0,n-1) 。后面就很好写了!(一点也不
    定义一个cur变量,它指的是下一次调用makeTree函数时的根节点,rootIdx则为目前树的根节点。那么后根遍历都知道是右→左→根,那么我们根据后根遍历构建树,根据中根遍历找左右子树,找到左右子树的边界以后递归调用makeTree函数即可。特别地,如果l>r,说明此树不存在;如果l==r,说明此节点是叶子节点,一一特判即可。这条题目以前我就不会写,今天放在这里,警戒自己学习一个。

    特别地,以前还看过这样类似的题型:已知前根遍历和后根遍历,求有几种可能的(二叉)树。我们之后讨论一下。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    
    #define f1(x,y) for(int x=1;x<=y;++x)
    #define f0(x,y) for(int x=0;x!=y;++x)
    #define bf1(x,y,z) for(int x=y;x>=z;--x)
    #define bf0(x,y,z) for(int x=y;x!=z;--x)
    typedef long long ll;
    typedef unsigned long long ull;
    int n;
    vector<int> inOrd,posOrd;
    int cur;
    
    struct Tree
    {
        int id;
        int lp,rp;
        Tree(int _i):id(_i),lp(-1),rp(-1) {}
    };
    vector<Tree> t;
    int makeTree(int l,int r)
    {
        if(l>r) return -1;
        int nowIdx=cur--;
        int rootIdx=0;
        for(int i=0;i!=inOrd.size();++i)
            if(inOrd[i]==posOrd[nowIdx])
            {
                rootIdx=i;break;
            }
        t.push_back(Tree(inOrd[rootIdx]));
        int np=t.size()-1;
        if(l==r)
            t[np].rp=t[np].lp=-1;
        else
        {
            int tmp=makeTree(rootIdx+1,r);
            t[np].rp=tmp;
            tmp=makeTree(l,rootIdx-1);
            t[np].lp=tmp;
        }
        return np;
    }
    int main()
    {
        cin>>n;
        int x;
        f0(i,n)
        {
            cin>>x;
            posOrd.push_back(x);
        }
        f0(i,n)
        {
            cin>>x;
            inOrd.push_back(x);
        }
        cur=n-1;
        int root=makeTree(0,n-1);
    
        queue<int> q;
        q.push(root);
        while(!q.empty())
        {
            int tr=q.front(); q.pop();
            if(tr==root) printf("%d",t[tr].id);
            else printf(" %d",t[tr].id);
            if(t[tr].lp!=-1) q.push(t[tr].lp);
            if(t[tr].rp!=-1) q.push(t[tr].rp);
        }
        printf("
    ");
    /*    for(int i=0;i!=5;++i)
        {
            printf("%d: %d<-self->%d
    ",t[i].id,((t[i].lp==-1)?-1:(t[t[i].lp].id)),((t[i].rp==-1)?-1:(t[t[i].rp].id)));
        }*/
        return 0;
    }
    如非注明,原创内容遵循GFDLv1.3发布;其中的代码遵循GPLv3发布。
  • 相关阅读:
    Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) J
    ROS_Kinetic_19 群机器人框架示例(micros swarm framework)
    ROS_Kinetic_18 使用V-Rep3.3.1和Matlab2015b(vrep_ros_bridge)续
    ROS_Kinetic_17 使用V-Rep3.3.1(vrep_ros_bridge)
    USB OTG原理+ ID 检测原理
    高通QSD MSM APQ区别
    qualcomm memory dump 抓取方法
    msm8974 camera driver添加新摄像头kernel hal修改
    现代控制理论-章节组织结构和仿真应用案例详细分析
    ROS_Kinetic_16 ubuntu中安装使用Matlab和ROS
  • 原文地址:https://www.cnblogs.com/samhx/p/pat-a1020.html
Copyright © 2011-2022 走看看