1127

简介: #include #include #include using namespace std;int n;const int maxn = 31;struct node { int data; node *l,...
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int n;
const int maxn = 31;
struct node { int data; node *l, *r;};
vector<int> post(maxn), in(maxn), ans, lnum(maxn);

node* build(int postL, int postR, int inL, int inR){//根据后序和中序建树
    if(postL > postR) return NULL;
    node *root = new node;
    root->data = post[postR];
    int k;
    for (k = inL; k <= inR; k++)
        if(in[k] == post[postR]) break;
    int numLeft = k - inL;
    root->l = build(postL, postL + numLeft - 1, inL, k - 1);
    root->r = build(postL + numLeft, postR - 1, k + 1, inR);
    return root;
}
void dfs(node *root, int depth){//求每一层的个数
    if (root == NULL) return;
    lnum[depth]++;
    if(root->l != NULL) dfs(root->l, depth + 1);
    if(root->r != NULL) dfs(root->r, depth + 1);
}
void bfs(node *root){//层序遍历的结果存在ans中
    queue<node*> q;
    q.push(root);
    while (!q.empty()) {
        node *now = q.front();
        q.pop();
        ans.push_back(now->data);
        if(now->l) q.push(now->l);
        if(now->r) q.push(now->r);
    }
}

int main(){
    cin >> n;
    for(int i = 0; i < n; i++) cin >> in[i];
    for(int i = 0; i < n; i++) cin >> post[i];
    node *root = build(0, n - 1, 0, n - 1);
    dfs(root, 0);
    bfs(root);
    //z输出
    int cnt = 0;
    printf("%d", ans[cnt++]);
    for (int i = 1; i < maxn;) {
        if (i % 2) {
            for (int j = 0; j < lnum[i]; j++)
                printf(" %d", ans[cnt+j]);
            cnt = cnt + lnum[i];
            i++;
        }else{
            for (int j = lnum[i] - 1; j >= 0; j--)
                printf(" %d", ans[cnt+j]);
            cnt = cnt + lnum[i];
            i++;
        }
    }
    cout << endl;

    return 0;
}
目录
相关文章
|
11月前
|
人工智能 Kubernetes 安全
阿里云 ACK 新升级,打造智算时代的现代化应用平台
阿里云 ACK 新升级,打造智算时代的现代化应用平台
70605 10
|
存储 消息中间件 API
RocketMQ实战:一个新的消费组初次启动时从何处开始消费呢?
RocketMQ实战:一个新的消费组初次启动时从何处开始消费呢?
RocketMQ实战:一个新的消费组初次启动时从何处开始消费呢?
|
运维 关系型数据库 MySQL
数据库选型最佳实践(二)
数据库选型最佳实践(二)
177 0
|
弹性计算 虚拟化 异构计算
阿里云GPU服务器A100、A10、V100、T4 GPU卡详解
阿里云GPU服务器A100、A10、V100、T4 GPU卡详解,阿里云GPU服务器租用价格表包括包年包月价格、一个小时收费以及学生GPU服务器租用费用,阿里云GPU计算卡包括NVIDIA V100计算卡、T4计算卡、A10计算卡和A100计算卡,GPU云服务器gn6i可享受3折优惠
15455 2
|
弹性计算 虚拟化 异构计算
阿里云GPU服务器NVIDIA A100 GPU卡租用价格表
阿里云GPU服务器NVIDIA A100 GPU卡租用价格表,阿里云GPU服务器租用价格表包括包年包月价格、一个小时收费以及学生GPU服务器租用费用,阿里云GPU计算卡包括NVIDIA V100计算卡、T4计算卡、A10计算卡和A100计算卡,GPU云服务器gn6i可享受3折优惠,阿里云百科分享阿里云GPU服务器租用价格表、GPU一个小时多少钱以及学生GPU服务器收费价格表
1801 0
阿里云GPU服务器NVIDIA A100 GPU卡租用价格表
|
消息中间件 存储 运维
阿里云消息队列 RocketMQ 5.0 全新升级:消息、事件、流融合处理平台
RocketMQ5.0 的发布标志着阿里云消息从消息领域正式迈向了“消息、事件、流”场景大融合的新局面。未来阿里云消息产品的演进也将继续围绕消息、事件、流核心场景而开展。
1152 1
阿里云消息队列 RocketMQ 5.0 全新升级:消息、事件、流融合处理平台
|
存储 算法 Java
CMS 触发GC(Allocation Failure)解析
针对GC中发生的"Allocation Failure"
812 0
|
弹性计算 固态存储 异构计算
阿里云GPU服务器收费标准价格表
2023阿里云GPU服务器收费标准价格表,阿里云GPU租用费用价格表,GPU计算卡包括NVIDIA V100计算卡、T4计算卡、A10计算卡和A100计算卡,GPU云服务器gn6i可享受3折优惠,阿里云百科分享阿里云GPU服务器学生优惠价格、GPU服务器收费价格表、GPU服务器多少钱一个小时等费用明细表:
6035 0
阿里云GPU服务器收费标准价格表
|
SQL 存储 JSON
基于 Delta Lake 构建数据湖仓体系
本文整理自阿里云开源大数据平台技术专家毕岩在7月17日阿里云数据湖技术专场交流会的分享。
基于 Delta Lake 构建数据湖仓体系
|
canal 消息中间件 关系型数据库
阿里MySQL binlog 增量订阅&消费组件canal介绍canal
canal [kə'næl],译意为水道/管道/沟渠,主要用途是基于 MySQL 数据库增量日志解析,提供增量数据订阅和消费
阿里MySQL binlog 增量订阅&消费组件canal介绍canal