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. 预备作业03:虚拟机安装及Linux操作系统练习

    安装虚拟机 安装时遇到的问题 在打开虚拟机时出现以下问题: 经查询该问题只需要更改其兼容性,使其"以管理员身份运行". 常用的Linux命令 $ who am i:查看当前伪终端的 ...

  2. 安装虚拟机和Linux系统的学习

    安装虚拟机和Linux系统的学习(随笔3) 1.安装虚拟机 首先我按着老师给的链接上的步骤一步一步安装VirtualBox,进行得十分顺利. 接着则是在虚拟机上安装Ubuntu. 然而安装完成以后按要 ...

  3. wmware 10 升级到11后,macos不能运行的问题

    解决方案: 1.由于wmware升级,原来的unlocker已不能使用. 所以得升级unlocker版本,目前支持wmware11的最新版本是2.0.4 http://www.insanelymac. ...

  4. 简单的Slony-I设置实例 II

    磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL集群方案相关索引页     回到顶级页面:PostgreSQL索引页 接前面例子, 简单的Slony-I设置实例 这次我 ...

  5. [agc006E]Rotate 3x3

    Description 给你一个3*N的网格,位置为(i,j)的网格上的数为i+3(j-1).每次选一个3*3的网格旋转180度,问最后能否使得网格(i,j)的值为ai,j.(5≤N≤105) 如图: ...

  6. 《Node.js 包教不包会》

    <Node.js 包教不包会> 为何写作此课程 在 CNode(https://cnodejs.org/) 混了那么久,解答了不少 Node.js 初学者们的问题.回头想想,那些问题所需要 ...

  7. 将 Python3 文件打包成 exe 文件

    我们用 Python 写好的代码,如何给别人在没有配置 Python 环境的情况下直接使用呢?尤其是面向 windows 众. 因为 Python 是一门解释性的语言,离开了 Python 解释器,P ...

  8. C# 调用C++ dll 返回char*调用方式(StringBuilder乱码)

    // CDLLDemo.cpp : 定义 DLL 应用程序的导出函数. // #include "stdafx.h" #include "string.h" # ...

  9. 搜索引擎ElasticSearch系列(五): ElasticSearch2.4.4 IK中文分词器插件安装

    一:IK分词器简介  IK Analyzer是一个开源的,基于java语言开发的轻量级的中文分词工具包.从2006年12月推出1.0版开始, IKAnalyzer已经推出了4个大版本.最初,它是以开源 ...

  10. LDPC译码算法代码概述

    程序说明 V0.0 2015/1/24 LDPC译码算法代码概述   概述   本文介绍了包括LDPC_Simulation.m, ldpcdecoderbp1.m,ldpcdecoderminsum ...