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基础——Oracle(二)
一.Oracle 中的几个服务 1.OracleDBConsoleorcl 进程:nmesrvc.exe oem控制台服务进程,dba用.Oracle Enterprise Manager(Oracl ...
- 【Java基础】2、Java中普通代码块,构造代码块,静态代码块区别及代码示例
Java中普通代码块,构造代码块,静态代码块区别及代码示例.Java中普通代码块,构造代码块,静态代码块区别及代码示例 执行顺序:静态代码块>静态方法(main方法)>构造代码块>构 ...
- jsp使用servlet实现文件下载
1.在index.jsp写入如下代码 <a href="demo2">下载</a> 2.在src中创建ServletDemo2类 public class ...
- 【读书笔记】iOS-storyBoard-为一个按钮添加一个点击事件
按照故事板的用语,应用中的一个界面屏幕被称作一个”场景(Scene)",以后添加额外的场景时,停靠区中将有另一个部分. 一,新建立一个工程,如图所示. 二,选中Main.storyboard ...
- 【读书笔记】iOS-离线可用的Web应用
众所周知,网页不光需要靠互联网接入访问才能提供各种形式的服务,而且连网页自身的各种设计元素也需要在有网接入的情况上才能获得. 但借助于HTML5的离线特性,我们可以通过把各种类型的资源都储存在Web应 ...
- Linux 下tomcat 出现 java.lang.OutOfMemoryError: unable to create new native thread
问题现象: Tomcat(8.5.13)部署了SuperMap iServer,并发用户在100左右.系统运行一段时间后,服务崩溃.异常提示 问题分析: 1.看到日志中的提示信息后,认为是系统内存不足 ...
- [Objective-C] 创建常量
新博客wossoneri.com #define宏定义 #define是一条预编译指令, 编译器在编译阶段前期会将所有使用到宏的地方简单地进行替换. 在预处理器里进行文本替换,没有类型,不做任何类型检 ...
- const int *p 和int * const p 的区别
看例子: int sloth = 3; const int *p1 = &sloth; int * p2 const = &sloth; 这样申明的话,不允许使用p1来修改sloth的 ...
- Linksys EA6500刷ddwrt成功记
网上刷Linksys EA6500的资料不多,然后又绕了好多个弯子,自己记录备忘. 首先EA6500有两个版本v1和v2,对应的固件不同. 区分方法: 1.v1的背后是两个颜色一样的usb2.0 2. ...
- python 从外部获取传入的参数
有时候我们在执行python程序的时需要接收到外部传入的参数 python的 sys.argv[]就能实现 # test.py import sys #引入模块 str = sys.argv[1]pr ...