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
 #include<cstdio>
#include<iostream>
#include<queue>
using namespace std;
typedef struct NODE{
struct NODE* lchild, *rchild;
int data;
}node;
int in[], post[];
int N;
node* create(int inL, int inR, int postL, int postR){
if(postL > postR){
return NULL;
}
int head = postR;
node * root = new node;
root->data = post[head];
int i;
for(i = inL; i <= inR; i++){
if(in[i] == post[head])
break;
}
int numL = i - inL;
root->lchild = create(inL, i - , postL, postL + numL - );
root->rchild = create(i + , inR, postL + numL, postR - );
return root;
}
void levelOrder(node* tree){
queue<node*> Q;
Q.push(tree);
node* temp;
int cnt = ;
while(Q.empty() == false){
temp = Q.front();
cnt++;
Q.pop();
printf("%d", temp->data);
if(cnt != N)
printf(" ");
if(temp->lchild != NULL)
Q.push(temp->lchild);
if(temp->rchild != NULL)
Q.push(temp->rchild);
}
}
int main(){
scanf("%d", &N);
for(int i = ; i < N; i++)
scanf("%d", &post[i]);
for(int i = ; i < N; i++)
scanf("%d", &in[i]);
node* tree = create(, N - , , N - );
levelOrder(tree);
cin >> N;
return ;
}

总结:

1、题意:给出后序遍历和中序遍历,求层序遍历。

2、层序遍历时不要忘记在访问完元素后pop。

3、创建二叉树结束的条件是后序遍历的区间为空(postL > postR)。

4、注意控制最后一个空格不要输出,可以设置一个计数器,为N时不输出空格。

A1020. Tree Traversals的更多相关文章

  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 A1020 Tree Traversals(25)

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

  4. PAT甲级——A1020 Tree Traversals

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

  5. [PAT] A1020 Tree Traversals

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

  6. A1020. Tree Traversals(25)

    这是一题二叉树遍历的典型题,告诉我们中序遍历和另外一种遍历序列,然后求任何一种遍历序列. 这题的核心: 建树 BFS #include<bits/stdc++.h> using names ...

  7. PAT_A1020#Tree Traversals

    Source: PAT A1020 Tree Traversals (25 分) Description: Suppose that all the keys in a binary tree are ...

  8. Tree Traversals

    Tree Traversals 原题链接 常见的二叉树遍历的题目,根据后序遍历和中序遍历求层次遍历. 通过后序遍历和中序遍历建立起一棵二叉树,然后层序遍历一下,主要难点在于树的建立,通过中序遍历和后序 ...

  9. HDU 1710 二叉树的遍历 Binary Tree Traversals

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

随机推荐

  1. 开发CMDB系统

    背景: 在现网环境中服务器多了每天服务器的配置 情况我们很难记住,当某台服务器硬件配置变化后可以第一时间了解,某台服务器出现问题时可以快速定位机架位置,之前都是excel文档,要查某项数据时极不方便. ...

  2. 基于Python的ModbusTCP客户端实现

    Modbus协议是由Modicon公司(现在的施耐德电气Schneider Electric)推出,主要建立在物理串口.以太网TCP/IP层之上,目前已经成为工业领域通信协议的业界标准,广泛应用在工业 ...

  3. linux-镜像下载

    https://blog.csdn.net/sinat_36564972/article/details/81560395 Centos6.5镜像下载 2018年08月10日 11:35:53 深夜搬 ...

  4. 第八周--Linux中进程调度与进程切换的过程

    [潘恒 原创作品转载请注明出处 <Linux内核分析>MOOC课程 "http://mooc.study.163.com/course/USTC 1000029000 " ...

  5. Linux内核分析 期末总结

    Linux内核分析 期末总结 一.知识概要 1. 计算机是如何工作的 存储程序计算机工作模型:冯诺依曼体系结构 X86汇编基础 会变一个简单的C程序分析其汇编指令执行过程 2. 操作系统是如何工作的 ...

  6. The Last Reader Response——13-17

    Hi, everybody, nice to meet you, this is probably my best and meet you at a time, in the past a seme ...

  7. Daily Scrum - 12/15-21

    Meeting Minutes 没有什么实质性进展: 添加/完成了一个新feature,即使用非线性的函数作为速度条的设定: 等待与travis开会,讨论下一步的feature = =: 阅读code ...

  8. FZU软工第五次作业-词组频率分析

    目录 00.前言: 01.分工: 02.PSP表格: 03.解题思路描述与设计实现说明: 解题思路简述: 关键代码 04.附加题设计与展示: 设计的创意独到之处 实现思路 实现成果展示 05.关键代码 ...

  9. 使用YII框架的migrate迁移数据库

    框架版本:2.0.13 官网手册说明:http://www.yiichina.com/doc/guide/2.0/db-migrations 创建迁移 命令的格式: php yii migrate/c ...

  10. [linux学习]sysctl 以及 net.ipv4.ip_forward

    1. sysctl 命令显示 当前系统的参数配置信息 显示全部配置信息 sysctl -a 帮助信息主要如下: [root@k8s-master ~]# sysctl -help Usage: sys ...