题目描述

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. Java中equals方法null造成空指针异常的原因及解决方案

    正文 bug描述 当查找已修改的与未修改的做比较的时候,之前为null的值调用equals方法与其他值做比较的时候,会导致抛出空指针异常 知识补充 关于null,你不得不知道的几件事: 1.null是 ...

  2. python 生成金字塔

    num = eval(input("请输入一个整数:")) , num + ): , -): print(" ", end="\t") , ...

  3. python 绘制五角星

    code import turtle n = eval(input("请输入五角星的长度")) turtle.begin_fill() #开始填充颜色 i = : turtle.f ...

  4. python国内镜像源

    让python pip使用国内镜像 国内源: 清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:http://mirrors.aliyun.com/pyp ...

  5. python3版 爬虫了解

    摘要:本文将使用Python3.4爬网页.爬图片.自动登录.并对HTTP协议做了一个简单的介绍.在进行爬虫之前,先简单来进行一个HTTP协议的讲解,这样下面再来进行爬虫就是理解更加清楚. 一.HTTP ...

  6. codeforces gym #101161H - Witcher Potion(状压DP)

    题目链接: http://codeforces.com/gym/101161/attachments 题意: 总共有n瓶药可供选择 每瓶药可以增加$e_i$点体力,和$p_i$点毒性 每分钟消耗1点毒 ...

  7. How to use reminder feature of the outlook

    https://support.office.com/en-us/article/set-or-remove-reminders-7a992377-ca93-4ddd-a711-851ef359792 ...

  8. How To Install OpenSSL on Windows

    转自 https://tecadmin.net/install-openssl-on-windows/ OpenSSL is a full-featured toolkit for the Trans ...

  9. 用python读取csv信息并写入新的文件

    import csv fo = open("result.txt", "w+") reader = csv.reader(open('test.csv')) f ...

  10. [转]SQL server 2008R2 中 C#Winfoirm 使用 SqlDependency 机制实现 数据库中某一张表的监视

    转自:https://blog.csdn.net/u012183487/article/details/77776930 System.Data.SqlClient命名空间下的 sqlDependen ...