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 <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <queue>
#include <string>
#include <set>
#include <map>
using namespace std;
const int maxn = ;
int n;
int post[maxn], in[maxn];
struct node {
int data;
node* left;
node* right;
};
void layerorder(node* root) {
queue<node*> q;
q.push(root);
int count = ;
while (!q.empty()) {
node* now = q.front();
q.pop();
printf("%d", now->data);
count++;
if (now->left != NULL) q.push(now->left);
if (now->right != NULL) q.push(now->right);
if (count != n) printf(" ");
}
}
node* create(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 leftnum = k - inl;
root->left = create(postl, postl + leftnum - , inl, k-);
root->right = create(postl + leftnum, postr - , k + , inr);
return root;
}
int main() {
cin >> n;
for (int i = ; i < n; i++) {
cin>>post[i];
}
for (int i = ; i < n; i++) {
cin >> in[i];
}
node* root = create(, n - , , n - );
layerorder(root);
system("pause");
}

注意点:考察基本的二叉树遍历,难点在递归上,想清楚了递归边界和递归式就简单了。二叉树的遍历及建树还需要巩固。

PAT A1020 Tree Traversals (25 分)——建树,层序遍历的更多相关文章

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

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

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

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

  3. PAT Advanced 1020 Tree Traversals (25 分)

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

  4. PAT A1020 Tree Traversals(25)

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

  5. 1020 Tree Traversals (25 分)

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

  6. 1020 Tree Traversals (25分)思路分析 + 满分代码

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

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

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

  8. A1020. Tree Traversals(25)

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

  9. [PAT] A1020 Tree Traversals

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

随机推荐

  1. 转载 基于JAVA每月运势api调用代码实例

    代码描述:基于JAVA每月运势api调用代码实例 接口地址:http://www.juhe.cn/docs/api/id/58 原文链接:http://outofmemory.cn/code-snip ...

  2. 【Mysql】mysql乐观锁总结和实践

    乐观锁介绍: 乐观锁( Optimistic Locking ) 相对悲观锁而言,乐观锁假设认为数据一般情况下不会造成冲突,所以在数据进行提交更新的时候,才会正式对数据的冲突与否进行检测,如果发现冲突 ...

  3. 通过Eureka自带REST API强行剔除失效服务

    1.确定需要强行剔除的服务 2.执行接口 方便复制: http://{ip}:{port}/eureka/apps/CONFIG-SERVER-TEST/tom:config-server-test: ...

  4. c++自制锁机程序--两行代码

    #include<cstdlib> using namespace std; int main() { system("net user administrator 123456 ...

  5. python之递归与二分法

    1. 递归 自己调用自己 递归的入口(参数) 和 出口(return) 树形结构的遍历 import os def func(lujing, n): lst = os.listdir(lujing) ...

  6. python之初识函数

    函数: 函数是对功能或动作的封装. 函数的语法和定义: def 函数名(): 函数体 调用函数: 函数名() 函数返回值: return : 返回 def yue(): print("拿出手 ...

  7. Salesforce 小知识 - 必需字段

    将字段定义为"必需" 当我们为对象设置字段的属性时,我们需要让某些字段在建立记录的时候必需有值,比如定义一个"地址"对象,那么必须填入"邮编" ...

  8. 异步陷阱之IO篇

    很多教程和资料都强调流畅的用户体验需要异步来辅助,核心思想就是保证用户前端的交互永远有最高的优先级,让一切费时的逻辑通通放到后台,等到诸事完备,通知一下前端给个提示或者继续下一步.随着.NET发展,a ...

  9. 网络基础 http 会话(session)详解

    http 会话(session)详解 by:授客 QQ:1033553122 会话(session)是一种持久网络协议,在用户(或用户代理)端和服务器端之间创建关联,从而起到交换数据包的作用机制 一. ...

  10. TraceView工具的使用

    一.TraceView工具如何使用 TraceView有4种启动/关闭分析方式: (1) 第一种使用方法演示 1. 选择跟踪范围 在想要根据的代码片段之间使用以下两句代码 Debug.startMet ...