PAT A1020 Tree Traversals (25 分)——建树,层序遍历
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 分)——建树,层序遍历的更多相关文章
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT Advanced 1020 Tree Traversals (25 分)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT A1020 Tree Traversals(25)
题目描述 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder ...
- 1020 Tree Traversals (25 分)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- 1020 Tree Traversals (25分)思路分析 + 满分代码
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
- 【PAT甲级】1020 Tree Traversals (25 分)(树知二求一)
题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...
- A1020. Tree Traversals(25)
这是一题二叉树遍历的典型题,告诉我们中序遍历和另外一种遍历序列,然后求任何一种遍历序列. 这题的核心: 建树 BFS #include<bits/stdc++.h> using names ...
- [PAT] A1020 Tree Traversals
[题目] distinct 不同的 postorder 后序的 inorder 中序的 sequence 顺序:次序:系列 traversal 遍历 题目大意:给出二叉树的后序遍历和中序遍历,求层次遍 ...
随机推荐
- Java学习笔记之——变量与数据类型、运算符
一.变量 1.变量:变化的值 变量在代码运行期间,开辟了一块空间 .这块空间是有地址的,给这块取了个名字, 这个名字就叫做变量名,这块空间放的东西叫做变量值 2.变量的初始化: (1)先声明再赋值: ...
- Java 多态 ——一个案例 彻底搞懂它
最近,发现基础真的hin重要.比如,Java中多态的特性,在学习中就是很难懂,比较抽象的概念.学的时候就犯糊涂,但日后会发现,基础在日常工作的理解中占有重要的角色. 下面,我将用一个代码实例,回忆和巩 ...
- python之锁, 队列
进程的其他方法 进程id,进程名字,查看进程是否活着is_alive() terminate()发送结束进程的信号 import time import os from multiprocessin ...
- svn和ftp的不同应用场合
作者:朱金灿 来源:http://blog.csdn.net/clever101 本来二者不是同一类的东西,是不能比较.引发我思考的是部门一些同事错把svn工具作为一个文件共享工具.因此我在思考一个问 ...
- JavaScript与正则表达式
正则表达式的定义 正则表达式与字符串对象相关的方法 相关示例 一.正则表达式(regular expression简称res) 1.定义: 一个正则表达式就是由普通字符以及特殊字符(称为元字符)组成 ...
- tinypng upload一键压缩上传工具,告别人肉
地址 项目地址:tinypng-upload 有兴趣的可以玩一玩,因为平时经常会用到图片压缩,上传,如果你也觉得很繁琐的话,这个将会解决你的痛点. 关于 tinypng-upload 这是一个基于 e ...
- 从零自学Java-8.创建第一个对象
1.创建对象:2.使用属性描述对象:3.确定对象的行为:4.合并对象:5.从其他对象继承:6.转换对象和其他类型的信息. 程序NewRoot2:计算输入数的算数平方根并输出 package com.j ...
- ER模型试题
M公司为了便于开展和管理各项业务活动,提高公司的知名度和影响力,拟构建一个基于网络的会议策划系统. [需求分析结果] 该系统的部分功能及初步需求分析的结果如下 : (1)M公司旗下有业务部.策划部和其 ...
- HTTP请求行、请求头、请求体详解(转)
转自 https://blog.csdn.net/u010256388/article/details/68491509/ HTTP请求报文解剖 HTTP请求报文由3部分组成(请求行+请求头+ ...
- CRM 各种类型字段的 赋值 取值
//lookupRecordEntity["new_lead"] = new EntityReference(entity.LogicalName, entity.Id); 获取时 ...