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的更多相关文章

  1. PAT甲级1127. ZigZagging on a Tree

    PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...

  2. PAT甲级 1127. ZigZagging on a Tree (30)

    1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  3. pat 甲级 1127. ZigZagging on a Tree (30)

    1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. PAT甲级1066. Root of AVL Tree

    PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...

  9. PAT 1127 ZigZagging on a Tree

    Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...

随机推荐

  1. python 装饰器和软件目录规范一

    1.装饰器和迭代器的概念. 装饰器本质是一个函数,是为其他函数添加附加功能. 原则:不修改原函数源代码 不修改原函数的调用方式 2.装饰器的简单应用 # Author : xiajinqi impor ...

  2. Simulating Mouse Events in JavaScript

    http://marcgrabanski.com/simulating-mouse-click-events-in-javascript/

  3. Linux入门第五天——shell脚本入门(下)基础语法之调试debug

    一.如何debug 1.通过sh命令的参数: sh [-nvx] scripts.sh 选项与参数: -n :不要执行 script,仅查询语法的问题: -v :再执行 sccript 前,先将 sc ...

  4. 20155304田宜楠-第三次作业:虚拟机的安装与Linux学习

    安装VirtualBox虚拟机 安装VirtualBox虚拟机 这一步很简单,参考老师给的教程一步步安装,很快就完成了. 2.安装Ubuntu 这一步可是让我吃尽了苦头,我按照老师给的下载地址成功下载 ...

  5. [agc006D]Median Pyramid Hard-[二分+乱搞]

    Description 题目大意:给你一个长度为n*2-1的排列,将除了该序列头尾的两个数外的其他数(设为i)变为原序列i-1,i,i+1下标数的中位数.求最后的数是什么.例子如下: Solution ...

  6. 【BZOJ4560】[NOI2016]优秀的拆分

    [BZOJ4560][NOI2016]优秀的拆分 题面 bzoj 洛谷 题解 考虑一个形如\(AABB\)的串是由两个形如\(AA\)的串拼起来的 那么我们设 \(f[i]\):以位置\(i\)为结尾 ...

  7. P3368 【模板】树状数组 2(区间增减,单点查询)

    P3368 [模板]树状数组 2 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数数加上x 2.求出某一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表 ...

  8. Tomcat安装部署和安全加固优化以及反向代理应用

    1.Tomcat介绍 Tomcat是Apache软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache.Sun和其他一些公司及个人共同 ...

  9. [SDOI2011]染色 树链剖分

    LG传送门 我写这道题的题解主要是因为洛谷上的题解要么讲的不清要么代码丑滑稽,导致初学时的我调了很久,所以想发个题解方便后来人. 由于要维护的信息还是具有区间可加性,只需要记录一下每个区间的左右端点颜 ...

  10. cf 448c Painting Fence

    http://codeforces.com/problemset/problem/448/C 题目大意:给你一个栅栏,每次选一横排或竖排染色,求把全部染色的最少次数,一个点不能重复染色. 和这道题有点 ...