PAT 甲级 1127 ZigZagging on a Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805349394006016
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
代码:
#include <bits/stdc++.h>
using namespace std; int N, root;
vector<int> in, post;
vector<int> ans[35];
int tree[35][2]; struct Node{
int index;
int depth;
}; void dfs(int &index, int ileft, int iright, int pleft, int pright) {
if(ileft > iright) return ;
index = pright;
int i = 0;
while(in[i] != post[pright]) i ++;
dfs(tree[index][0], ileft, i - 1, pleft, pleft + i - ileft - 1);
dfs(tree[index][1], i + 1, iright, pleft + i - ileft, pright - 1);
} void bfs() {
queue<Node> q;
q.push(Node{root, 0});
while(!q.empty()) {
Node temp = q.front();
q.pop();
ans[temp.depth].push_back(post[temp.index]);
if(tree[temp.index][0])
q.push(Node{tree[temp.index][0], temp.depth + 1});
if(tree[temp.index][1])
q.push(Node{tree[temp.index][1], temp.depth + 1});
}
} int main() {
scanf("%d", &N);
in.resize(N + 1), post.resize(N + 1);
for(int i = 1; i <= N; i ++)
scanf("%d", &in[i]);
for(int i = 1; i <= N; i ++)
scanf("%d", &post[i]); dfs(root, 1, N, 1, N);
bfs(); printf("%d", ans[0][0]);
for(int i = 1; i < 35; i ++) {
if(i % 2) {
for(int j = 0; j < ans[i].size(); j ++)
printf(" %d", ans[i][j]);
} else {
for(int j = ans[i].size() - 1; j >= 0; j --)
printf(" %d", ans[i][j]);
}
} return 0;
}
可能敲了一万遍才敲对吧 今天是被 Tizzy T 洗脑的一天
PAT 甲级 1127 ZigZagging on a Tree的更多相关文章
- PAT甲级1127. ZigZagging on a Tree
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
- 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 (30)
1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- 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 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 1127 ZigZagging on a Tree[难]
1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive in ...
- 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 ...
- PAT甲级1066. Root of AVL Tree
PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...
- PAT 1127 ZigZagging on a Tree
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...
随机推荐
- gi的安装和使用
Git的安装 git是什么? git是一种版本控制器,更直白的说,团队开发的时候,管理代码使用的软件 Linux下的安装 yum install git Git的配置 在使用git之前,需要先进行配置 ...
- Debian Linux 下安装pip3
由于Debian自带了python3,于是只需要安装pip 1.首先安装setuptools 下载wget --no-check-certificate https://pypi.python.org ...
- scala (5) 可变序列和不可变序列
/** * 序列分为可变长和不可变长,序列其实就是list,底层是链表结构 * 特点:插入有序,可重复,增加和移除元素很快,查询慢 * 不可变长序列:List * 可变长序列:ListBuffer * ...
- 20155206 《JAVA程序设计》实验二(JAVA面向对象程序设计)实验报告
20155206 <JAVA程序设计>实验二(JAVA面向对象程序设计)实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S ...
- 20155222 2016-2017-2 《Java程序设计》第9周学习总结
20155222 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,开发人员使用JDBC的标 ...
- 学号20155311 2016-2017-2 《Java程序设计》第10周学习总结
学号20155311 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据 计算机网络 路 ...
- MSP430编译器__intrinsic指令
1. 在文件intrinsics.c里面发现很多函数前面有__intrinsic,说是这些是MSP430的特定函数(其他单片机用不了),应该和编译器有关,并没有具体的函数实现,我猜测,是直接转成汇编代 ...
- Maven学习(十)-----使用Maven创建Java项目
所需要的工具: Maven 3.3.3 Eclipse 4.2 JDK 8 注意:请确保 Maven 是正确安装和配置(在Windows,*nix,Mac OSX系统中),然后再开始本教程,避免 mv ...
- 使用phpMyAdmin管理网站数据库(创建、导入、导出…)
作为一名站长,最重视的就是网站的数据安全了.本节襄阳网站优化就来讲讲如何使用phpMyAdmin管理软件进行mysql数据库的管理,实现基本的数据库管理用户.数据库的创建.数据的导入和导出操作(网站备 ...
- python装饰器简单使用
装饰器和闭包关联很大,要先明白闭包是什么 原始代码: def foo(): print('fcc') 增加装饰器 from time import ctime,sleep def w(fcc): de ...