pat 甲级 1127. ZigZagging on a Tree (30)
1127. ZigZagging on a Tree (30)
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple standard routine to print the numbers in level-order. However, if you think the problem is too simple, then you are too naive. This time you are supposed to print the numbers in "zigzagging order" -- that is, starting from the root, print the numbers level-by-level, alternating between left to right and right to left. For example, for the following tree you must output: 1 11 5 8 17 12 20 15.

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 inorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print the zigzagging sequence of the tree in a line. 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:
8
12 11 20 17 1 15 8 5
12 20 17 11 15 8 5 1
Sample Output:
1 11 5 8 17 12 20 15 题意:通过后序遍历中序遍历复原二叉树,再按照题目要求的顺序输出,输出要求只要对层序遍历的方式稍加改动即可。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<set>
#include<queue>
#include<map>
using namespace std;
#define INF 0x3f3f3f
#define N_MAX 30000+5
typedef long long ll;
struct Node {
int key=INF, L, R;
Node() {}
Node(int key,int l,int r):key(key),L(l),R(r) {}
}node[N_MAX]; vector<int> in, post;int n, cnt;
void dfs(int n,int l,int r) {
if (l>r) {
node[n].key = INF;
return;
}
int root = post[cnt--];
node[n] = Node(root, * n+, * n + );
int k = find(in.begin(),in.end(),root)-in.begin();
dfs( * n + , k + , r);
dfs( * n+, l, k - );
}
int order[N_MAX];
vector<int>res; vector<int>level;
void bfs(int root) {
queue<int>que;
que.push(root);
order[root] = ;
while (!que.empty()) {
int p = que.front(); que.pop();
if (node[p].key != INF) {
res.push_back(node[p].key);
level.push_back(order[p]);
if (node[p].L != ) { order[node[p].L] = order[p] + ;que.push(node[p].L); }
if (node[p].R != ) { order[node[p].R] = order[p] + ;que.push(node[p].R); }
}
}
} int main() {
while (scanf("%d",&n)!=EOF) {
in.resize(n); post.resize(n);
for (int i = ; i < n; i++)scanf("%d",&in[i]);
for (int i = ; i < n; i++)scanf("%d", &post[i]);
cnt = n - ;
dfs(, , n - );
bfs();
int num = ,orde=;//num是每一层的计数器
vector<int>out;
for (int i = ; i < res.size();) {
out.clear();
while (i<res.size()&&orde ==level[i]) {
out.push_back(res[i]);
i++;
}
if (orde & ) {
for (int j = ; j < out.size(); j++)printf("%d%s", out[j], (i == res.size()&&j+==out.size())? "\n" : " ");
}
else {
for (int j = out.size() - ; j >= ; j--)printf("%d%s", out[j], (i == res.size()&&j== )? "\n" : " ");
}
orde++;
} }
}
pat 甲级 1127. ZigZagging on a Tree (30)的更多相关文章
- PAT甲级 1127. ZigZagging on a Tree (30)
1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT甲级1127. ZigZagging on a Tree
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
- PAT 甲级 1127 ZigZagging on a Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805349394006016 Suppose that all the k ...
- PAT Advanced 1127 ZigZagging on a Tree (30) [中序后序建树,层序遍历]
题目 Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree c ...
- pat 甲级 1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a bin ...
- PAT甲级——A1127 ZigZagging on a Tree【30】
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...
- PAT甲题题解-1127. ZigZagging on a Tree (30)-中序、后序建树
根据中序遍历和前序遍历确定一棵二叉树,然后按“层次遍历”序列输出.输出规则:除根节点外,接下来每层的节点输出顺序是:先从左到右,再从右到左,交替输出 #include <iostream> ...
- 1127 ZigZagging on a Tree (30 分)
1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive in ...
随机推荐
- ElasticSearch High Level REST API【5】使用模板搜索
ElasticSearch Rest高级API 提供了多种搜索方式,除了前面讲到的search查询,ElasticSearch 还提供了通过模板搜索查询.我个人比较喜欢这种方式. 我们可以通过脚本预选 ...
- MultipartFile 动态决定是否上传文件,解决不上传文件报错
controller 接收参数 用 HttpServletRequest 代替 @RequestParam() 接收参数 picFile 前台 传文件的参数名字 , 这样 前段 传 nul ...
- pycahrm git配置笔记
1. 在file - setting - plugins 中查看是否有github插件, 此处是用于处理插件位置
- 第1 章初识Python
1.print()—输出 print()函数的基本用法如下: print(输出内容) 其中,输出内容可以是数字和字符串(使用引号括起来),此类内容将直接输出,也可以是包含运算符的表达式,此类内容将计算 ...
- tcl之list操作
- java util - 中文、繁体转成拼音工具pinyin4j
需要 pinyin4j-2.5.0.jar 包 代码例子 package cn.java.pinyin4j; import net.sourceforge.pinyin4j.PinyinHelper; ...
- MYSQL 自定义排序
在mysql order by排序中,大多数情况下仅使用默认排序规则就够了:字符串按字典顺序,数字按大小等等.可有时候,某个字段是有自身业务含义的,比如 type(1,2,3)可能表示早/中/晚,如果 ...
- CentOS6.5生产环境系统安装
CentOS 6.5系统安装 1-1 将预先准备的CentOS 6.5安装光盘插入光驱中,开机/重启系统时,系统会进行自检,自检完毕就会出现安装系统时的引导界面,如图1-1所示.1-2 使用键盘方向键 ...
- (转)零基础入门深度学习(6) - 长短时记忆网络(LSTM)
无论即将到来的是大数据时代还是人工智能时代,亦或是传统行业使用人工智能在云上处理大数据的时代,作为一个有理想有追求的程序员,不懂深度学习(Deep Learning)这个超热的技术,会不会感觉马上就o ...
- git 远程仓库 与本地项目 挂钩 全过程
摘要:学了Android 快三个月了,依旧不会git,真的有些丢人.git是一个非常棒的团队协作的工具.其实也是分分钟的事情.Follow Me! Step 1 在码云上新建一个项目,作为远程仓库.里 ...