题目

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.

Input Specification:

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.

Output Specification:

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.

Sample Input:

7

2 3 1 5 7 6 4

1 2 3 4 5 6 7

Sample Output:

4 1 6 3 5 7 2

说白了就是,给你一棵二叉树的后序遍历结果和中序遍历结果,让你输出这棵二叉树的层序遍历结果。

题解:

我们应该都接触过由中序遍历和先序遍历得到后序遍历,或者由中序遍历和后序遍历得到先序遍历,其实只要你根据给出的两个序列重构出这棵二叉树,层序遍历也就得到了。

但重构二叉树需要自己创建二叉树的数据结构,还要造一棵树出来,其中指针操作又容易出错,我不想创建树可以吗?可以的!题目只是让我输出最后的遍历结果,又不是让我得到一棵树,我只需要巧妙的调整一下代码就能利用一个map<int, int>得到层序遍历的结果(柳婼大神nb

但不管怎么样,都得先解决一个问题,就是利用中序序列和后序序列怎么得到二叉树呢?这里我简单说一下吧,相信大多数人都会这个操作的。

给个例子:

中序遍历: A D E F G H M Z

后序遍历: A E F D H Z M G

画树求法:

第一步,根据后序遍历的特点,我们知道后序遍历最后一个结点即为根结点,即根结点为G。

第二步,观察中序遍历ADEFGHMZ。其中root节点G左侧的ADEF必然是root的左子树,G右侧的HMZ必然是root的右子树。

第三步,根据后序遍历(左右中)的特点,后序遍历中,根G的左边的节点M必然是它的右子树HMZ的根。

第四步,右子树HMZ有三个节点,根G往左移三个位置后D必然是它的左子树的根

第五步,观察发现,上面的过程是递归的。先找到当前树的根节点,然后划分为左子树,右子树,然后进入左子树重复上面的过程,然后进入右子树重复上面的过程。最后就可以还原一棵树了。该步递归的过程可以简洁表达如下:

1 确定根,确定左子树,确定右子树。

2 在左子树中递归。

3 在右子树中递归。

4 返回或者打印当前根。

层序遍历

那我们一般是怎么是层序遍历,宽度优先遍历,层序其实也就是按顺序,一般都是用一个队列,队列中保存的始终是一整层的姐弟啊,对于每层每个节点,访问自己,把左右孩子加入队列,【保证了访问的先后顺序】

我们这里采用键值对map<int, int>的形式保存,值:节点值,键:层序遍历下它实际的访问序号(下标),对于每个节点,假如编号是i,那么它的左孩子编号是2 * i,右孩子编号是 2*i+1因为键值的大小关系,map会自动按其顺序排序,相当于保证了访问的先后顺序,这样我们最后直接按顺序输出整个map的值,就实现了队列的功能。

但是因为 我们的序列是数组,下标是从0开始的,所以为了避免 2 * 0 = 0节点值覆盖,我们左孩子编号是 2*i+1,右孩子编号是 2*i+2,当然你可以选择让根节点编号是1,就不会有这个问题。

代码实现

#include <iostream>
#include <vector>
#include <map>
using namespace std; // 后续遍历,中序遍历
vector<int> postorder, inorder;
// 层序遍历
map<int, int> level_order; // 因为层序其实也就是按顺序,一般都是用一个队列,对于每层节点,访问自己,把左右孩子加入队列,【保证了访问的先后顺序】
// 我们这里采用键值对<int, int>的形式保存,值:节点值,键:层序遍历下它实际的访问序号(下表)
// 对于每个节点,假如编号是i,那么它的左孩子编号是2 * i,有孩子编号是 2 * i + 1
// 但是因为 我们的下标是从0开始的,所以为了避免 2 * 0 = 0, 我们左孩子编号是 2*i+1,有孩子编号是 2*i+2
// 这样,因为键值的大小关系,map会自动按其顺序排序,相当于保证了访问的先后顺序,这样我们最后直接输出map即可 /**
* in_start 当前子树中序遍历序列在 inorder数组中的起始位置
* in_end 当前子树中序遍历序列在 inorder数组中的结束位置
* post_index 当前子树树根在 postorder数组中的下标
*
* 我们在递归时,每次找到的都是当前树根,然后分别去它的左右子树递归
* 所以我们增加参数 index 保存当前找到的 树根 按层序遍历时它实际的编号(第几个访问),
* 在进入它的左子树时,编号就成了 2 * inedx + 1,递归右子树时 编号就成了 2 * index + 2
*/
void Preorder(int in_start, int in_end, int post_end, int index) {
// 当前子树构建完毕,返回
if (in_start > in_end) return;
int i = in_start;
// post_end总是表示当前子树树根在整棵树后序遍历数组中的下标
// 找到当前子树树根在中序遍历序列中的位置
while (i <= in_end && inorder[i] != postorder[post_end]) i++;
// i 把 [in_start, in_end]划分为 [in_start, i - 1] [i + 1, in_end]两部分
// 其中 [in_start, i - 1] 是当前树根的左子树的中序遍历序列在整棵树的中序遍历序列inorder中的范围
// 其中 [i + 1, in_end] 是当前树根的右子树的中序遍历序列在整棵树的中序遍历序列inorder中的范围
// 根据后续遍历特点(左右中) x x x x x root,当前子树的根是 root,那么 root前一位置就是当前子树的右子树的根
// 加入 右子树有 n个节点,那么 root 往左 n 个位置就是 左子树的树根
level_order[index] = postorder[post_end];
Preorder(in_start, i - 1, post_end - 1 - (in_end - i), 2 * index + 1);
Preorder(i + 1, in_end, post_end - 1, 2 * index + 2);
} int main() { // n个节点
int n;
cin >> n;
// 重新分配大小
postorder.resize(n);
inorder.resize(n);
// 读入后序遍历结果
for (int i = 0; i < n; ++i) cin >> postorder[i];
// 读入中序遍历结果
for (int i = 0; i < n; ++i) cin >> inorder[i];
// 得到层序
Preorder(0, n - 1, n - 1, 0);
// 遍历,注意输出格式,末尾不能有多余空格
auto it = level_order.begin();
cout << it->second;
while (++it != level_order.end())
cout << " " << it->second;
return 0;
}

我的代码其实就是看完柳婼大神的博客后写的,只是她的题解思路很简洁,我的脑子不太够用,就往详细的写了写,如果大家看着繁琐就直接去访问柳神的博客吧!

1020 Tree Traversals (25分)思路分析 + 满分代码的更多相关文章

  1. 1025 PAT Ranking (25分) 思路分析 +满分代码

    题目 Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of ...

  2. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  3. PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  4. PAT Advanced 1020 Tree Traversals (25 分)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  5. 1018 Public Bike Management (30分) 思路分析 + 满分代码

    题目 There is a public bike service in Hangzhou City which provides great convenience to the tourists ...

  6. 【PAT甲级】1020 Tree Traversals (25 分)(树知二求一)

    题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...

  7. 1020 Tree Traversals (25 分)

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

  8. 【PAT】1020 Tree Traversals (25)(25 分)

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  9. PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]

    题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...

随机推荐

  1. 深度学习之文本分类模型-前馈神经网络(Feed-Forward Neural Networks)

    目录 DAN(Deep Average Network) Fasttext fasttext文本分类 fasttext的n-gram模型 Doc2vec DAN(Deep Average Networ ...

  2. SpringBoot系列(九)单,多文件上传的正确姿势

    SpringBoot系列(九)分分钟解决文件上传 往期推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配 ...

  3. 详解 Web基本概念

    作为本专栏的第一篇博文,本人将带领同学们初步了解什么是Web,以及有关Web学习的一些基本知识点 那么,话不多说,开始主题的讲解吧: 首先,本人来解释下什么是Web: 概念: 使用浏览器进行访问的应用 ...

  4. PHP 将字符串转换为字符集格式UTF8/GB2312/GBK 函数iconv()

     iconv()介绍 iconv函数可以将一种已知的字符集文件转换成另一种已知的字符集文件 iconv('要转化的格式',‘转化后的格式’,‘转化的数据’); 但是转化是经常出错,一般需要在转成的编码 ...

  5. Canvas(3)---绘制饼状图

    Canvas(3)---绘制饼状图 有关canvas之前有写过两篇文章 1.Canvas(1)---概述+简单示例 2.Canvas(2)---绘制折线图 在绘制饼状图之前,我们先要理解什么是圆弧,如 ...

  6. 总结vscode调试vue,nodejs的各种方法

    之前写项目一直都是console.log()来调试的,浪费了很多时间,现在整理一下用vscode对nuxt(vue)前后端进行调试的方法 前端的调试 chrome+launch 使用chrome调试, ...

  7. tensorflow1.0 变量加法

    import tensorflow as tf state = tf.Variable(0,name='counter') print(state.name) one = tf.constant(1) ...

  8. 使用docker安装codimd,搭建你自己的在线协作markdown编辑器

    目录 一.前言 二.codimd是什么? 2.1 源于hackmd的超好用markdown编辑器 2.2 codimd的作用 三.安装和使用 3.1 安装前需要知道的 3.2 安装步骤 3.2.1 创 ...

  9. 开发AI+诊疗生发系统,「先锋汇美」借力人工智能辅助诊疗实现头皮医学检测...

    困扰年轻人的脱发问题萌生了新兴的产业链.36氪先前曾剖析过近来火热的植发市场,更多人则选择"防范于未然","头皮检测"服务备受关注.此前,人们对"头皮 ...

  10. [mysql]linux mysql 读写分离

    [mysql]linux mysql 读写分离 作者:flymaster qq:908601287 blog:http://www.cnblogs.com/flymaster500/ 1.简介 当今M ...