题目描述

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.
给出一个二叉树的后序遍历序列和中序遍历序列,求出这棵二叉树的层序遍历序列。

输出格式

Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

输出格式

For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

输入样例

7
2 3 1 5 7 6 4
1 2 3 4 5 6 7

输出样例

4 1 6 3

《算法笔记》中AC答案

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std; const int maxn = 50; struct node {
int data;
node* lchild;
node* rchild;
}; int pre[maxn], in[maxn], post[maxn]; //先序, 中序, 后序
int n; // 结点个数 // 当前二叉树的后序序列区间为[postL, postR], 中序序列区间为[inL, inR]
// create 函数返回构建出的二叉树的根结点地址
node* create(int postL, int postR, int inL, int inR) {
if(postL > postR) {
return NULL; // 后序序列长度小于等于0时,直接返回
}
node* root = new node; // 新建一个新的结点,用来存放当前二叉树的根结点
root->data = post[postR]; //新结点的数据域为根结点的值
int k;
for(k = inL; k <= inR; k++) {
if(in[k] == post[postR]) { // 在中序序列中找到in[k] == pre[L]的结点
break;
}
}
int numLeft = k - inL; // 左子树的结点个数
// 返回左子树的根结点地址,赋值给root的左指针
root->lchild = create(postL, postL + numLeft - 1, inL, k - 1);
// 返回右子树的根结点地址,赋值给root的右指针
root->rchild = create(postL + numLeft, postR - 1, k + 1, inR);
return root; // 返回根结点地址
} int num = 0; // 已输出的结点个数
void BFS(node* root) {
queue<node*> q; //注意队列里是存地址
q.push(root); // 把根结点地址入队
while(!q.empty()) {
node* now = q.front(); // 取出队首元素
q.pop();
printf("%d", now->data);
num++;
if(num < n) printf(" ");
if(now->lchild != NULL) q.push(now->lchild); // 左子树非空
if(now->rchild != NULL) q.push(now->rchild); // 右子树非空
}
} int main() {
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%d", &post[i]);
}
for(int i = 0; i < n; i++) {
scanf("%d", &in[i]);
}
node* root = create(0, n - 1, 0, n - 1); // 建树
BFS(root); // 层序遍历
return 0;
}

PAT A1020 Tree Traversals(25)的更多相关文章

  1. PAT A1020 Tree Traversals (25 分)——建树,层序遍历

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  2. A1020 Tree Traversals (25 分)

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  3. PAT 1060 爱丁顿数(25)(STL-multiset+思路)

    1060 爱丁顿数(25 分) 英国天文学家爱丁顿很喜欢骑车.据说他为了炫耀自己的骑车功力,还定义了一个"爱丁顿数" E ,即满足有 E 天骑车超过 E 英里的最大整数 E.据说爱 ...

  4. PAT 1065 单身狗(25)(STL-map+思路+测试点分析)

    1065 单身狗(25 分) "单身狗"是中文对于单身人士的一种爱称.本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱. 输入格式: 输入第一行给出一个正整数 N(≤  ...

  5. PAT 1070 结绳(25)(代码)

    1070 结绳(25 分) 给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下图所示套接在一起.这样得到的绳子又被当成是另一段绳子,可以再次对折去跟另一段绳子串连.每 ...

  6. Binary Tree Traversals(HDU1710)二叉树的简单应用

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  7. 1020 Tree Traversals (25 分)(二叉树的遍历)

    给出一个棵二叉树的后序遍历和中序遍历,求二叉树的层序遍历 #include<bits/stdc++.h> using namespace std; ; int in[N]; int pos ...

  8. PAT 2-10. 海盗分赃(25)

    题目链接:http://www.patest.cn/contests/ds/2-10 解题思路:参考:http://blog.csdn.net/linsheng9731/article/details ...

  9. [PAT] A1020 Tree Traversals

    [题目] distinct 不同的 postorder 后序的 inorder 中序的 sequence 顺序:次序:系列 traversal 遍历 题目大意:给出二叉树的后序遍历和中序遍历,求层次遍 ...

随机推荐

  1. Transformer模型总结

    Transformer改进了RNN最被人诟病的训练慢的缺点,利用self-attention机制实现快速并行. 它是由编码组件.解码组件和它们之间的连接组成. 编码组件部分由一堆编码器(6个 enco ...

  2. Codeforces 126B. Password(KMP,DP)

    Codeforces 126B. Password 题意:一个字符串,找出最长的子串t,它既是前缀又是后缀,还出现在中间.输出t,不存在则输出Just a legend. 思路:利用KMP算法处理出n ...

  3. BOM:浏览器对象

    1.window是javascript的顶级对象        window有很多内置方法 如 alert open 自定义函数 2.window是bom的顶级对象,通常情况下,可以省略 window ...

  4. 利用Apache shiro SimpleHash 加密字符串

    1.导入包 import org.apache.shiro.crypto.hash.SimpleHash; 1 2.代码 import org.apache.shiro.crypto.hash.Sim ...

  5. Zookeeper系列(十)zookeeper的服务端启动详述

    作者:leesf    掌控之中,才会成功:掌控之外,注定失败.出处:http://www.cnblogs.com/leesf456/p/6105276.html尊重原创,大家功能学习进步:  一.前 ...

  6. flask 第十篇 after_request before_request

    Flask我们已经学习很多基础知识了,现在有一个问题 我们现在有一个 Flask 程序其中有3个路由和视图函数,如下: from flask import Flask app = Flask(__na ...

  7. 黑马vue---18、v-for指令的四种使用方式

    黑马vue---18.v-for指令的四种使用方式 一.总结 一句话总结: (item, i) in list:什么in什么的形式,前面是各种参数 1.v-for循环普通数组? <p v-for ...

  8. 深度解析 Qt 中动态链接库

    本文介绍的是Qt 中动态链接库,现在有些软件有自动升级功能,有些就是下载新的DLL文件,替换原来的动态链接库,MFC好象也有类似机制,Qt还有一种方式,就是把一个QWidget子类,编译成动态链接库. ...

  9. Qt串口通信专题教程

    查看以前的教程:Qt编写串口通信程序全程图文讲解 查看Wincom和Lincom介绍:Qt跨平台串口通信软件Wincom与Lincom 下载软件,文档和源码:资源下载 ——————————————20 ...

  10. linux系统空间不足,不重启进程,清理僵尸文件。

    问题:通过lsof |grep delete命令可以看到状态为delete的进程文件占用了较多的空间,导致系统磁盘空间不足,而du 命令看到的磁盘空间占用没那么高. 得到僵尸文件名称:catalina ...