• 2020.11 程序设计天梯赛 L2-3 完全二叉树的层序遍历 (25分)


    L2-3 完全二叉树的层序遍历 (25分)

    题意:给出一棵完全二叉树的后序遍历,输出对应层序遍历。

    思路:对于给定的后序遍历序列,可以知道最后一个元素是树的根节点,可以使用递归建树。

    1.若当前结点的右子树不为空则继续将右子树遍历,若右子树为空则判断当前结点是否存在右儿子,右儿子的编号为 this.num*2+1,如果右儿子的编号小于等于结点的数量n则表示存在右儿子,在此新建结点。

    2.若右子树不满足条件则考虑左子树,同样是假如当前结点的左子树不为空则继续向左子树遍历,否则判断当前结点是否存在左儿子,左儿子的编号为 this.num*2,如果左儿子的编号小于等于结点的数量n则表示存在左儿子,在此新建结点。

    3.使用队列实现层序遍历。

    代码:

    #include <set>
    #include <map>
    #include <list>
    #include <stack>
    #include <queue>
    #include <deque>
    #include <cmath>
    #include <string>
    #include <vector>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <sstream>
    #include <iostream>
    #include <algorithm>
    //#include <unordered_map>
    #define INF 0x3f3f3f3f
    #define ll long long
    #define ull unsigned long long
    #define FILL(a,n,v) fill(a,a+n,v)
    #define Mset(a,v) memset(a,v,sizeof a)
    #define Mcpy(a,b) memcpy(a,b,sizeof b) //a=b
    #define fcio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
    #define fcin freopen("in.txt","r",stdin)
    #define fcout freopen("out.txt","w",stdout)
    #define mian main
    #define ture true
    #define esle else
    #define falg flag
    using namespace std;
    int n;
    int a[35];
    
    struct node
    {
        int value;
        int level;
        node* l;
        node* r;
    
        node(int value)
        {
            this->value=value;
            this->l=NULL;
            this->r=NULL;
            this->level=1;
        }
    };
    
    bool flag;
    void build(node* root,int a,int num)
    {
        if(flag) return ;
    
        if(root->r) build(root->r,a,num*2+1);
        else if(num*2+1<=n)
        {
            flag=true;
            root->r=new node(a);
            return;
        }
    
        if(flag) return ;
    
        if(root->l) build(root->l,a,num*2);
        else if(num*2<=n)
        {
            flag=true;
            root->l=new node(a);
            return;
        }
    }
    
    int res[50];
    int cnt=0;
    
    void levelOrder(node* root)
    {
        queue<node*>q;
        node* now=root;
        q.push(now);
        
        while(q.size())
        {
            now=q.front();
            res[cnt++]=now->value;
            q.pop();
            if(now->l) q.push(now->l);
            if(now->r) q.push(now->r);
        }
    }
    
    int main()
    {
        cin>>n;
        for(int i=1;i<=n;i++) cin>>a[i];
        reverse(a+1,a+n+1);
    
        node* root=new node(a[1]);
    
        for(int i=2;i<=n;i++)
        {
            flag=false;
            build(root,a[i],1);
        }
        levelOrder(root);
        for(int i=0;i<cnt;i++)
        {
            cout<<res[i]<<(i==n-1?'
    ':' ');
        }
    }
    
    
    
    
    
    
  • 相关阅读:
    qt 学习(三)消息基础
    qt学习(二)控件
    sqlserver学习_01
    java对文件操作--01
    js 将json字符串转换为json对象的方法解析
    实现动态代理(Java和spring)
    mysql_01_游标的使用
    java实现多文件上传01
    oracle-2_dblink的创建和使用
    sqlserver学习3---sql函数
  • 原文地址:https://www.cnblogs.com/lihahahahaji/p/14068304.html
Copyright © 2020-2023  润新知