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. C语言顺序队列

    顺序队列是一种只能在一头进和另一头出的数据结构,所以结构体里设2个指针分别指向头部和尾部,用数组来存储数据. #define MAXSIZE 1024 typedef int elemtype; ty ...

  2. Titanic幸存预测分析(Kaggle)

    分享一篇kaggle入门级案例,泰坦尼克号幸存遇难分析. 参考文章: 技术世界,原文链接 http://www.jasongj.com/ml/classification/ 案例分析内容: 通过训练集 ...

  3. WPF几个基础概念的浅显理解

    1.逻辑树与视觉树 逻辑树在结构上与xaml文件对应 视觉树更细化,拆分到控件的每个组成部分 2.依赖属性与附加属性 依赖属性:就是自己自己没有属性值,而是通过Binding从数据源获得值,就是依赖在 ...

  4. MySQL高级——课程大纲

    一.课程概述 总体结构概述: //特别注意本次课程目标在于写出高效的JAVA代码,而非DBA等的专业调优 各章节概述         

  5. 20155220 2016-2017-2 《java程序设计》第二周学习总结

    教材学习内容总结 3.1类型.变量与运算符 1)基本类型 byte 字节型 1 byte short 短整型 2 bytes int 整型 4 bytes long 长整型 8 bytes float ...

  6. 20155320《Java程序设计》实验一(Java开发环境的熟悉)实验报告

    20155320<Java程序设计>实验一(Java开发环境的熟悉)实验报告 实验内容及步骤 (一)命令行下Java程序开发 步骤一:首先在cmd中输入d:和cd ljq20155320进 ...

  7. Ubuntu + apache + Mysql +php

    发生了乱码问题: 打开apache配置文件: sudo gedit /etc/apache2/apache2.conf,在最后面加上:AddDefaultCharset UTF-8,如果还乱码,再将U ...

  8. [arc062E]Building Cubes with AtCoDeer

    Description 传送门 Solution 这道题直接暴力就好..毕竟只要枚举了前后两个瓷砖的方向和编号,其他瓷砖的颜色就是确定的了. 然而场上我的去重除了问题qaq. 我们钦定在立方体最前面的 ...

  9. .net core 无法获取本地变量或参数的值,因为它在此指令指针中不可用,可能是因为它已经被优化掉了

    使用vs 发布.net CORE 项目,调试遇到了“无法获取本地变量或参数的值,因为它在此指令指针中不可用,可能是因为它已经被优化掉了”这个问题,弄了半天才发现是发布的时候没有设置为debug,做个总 ...

  10. 搭建 ssm 环境

    <!-- 引入外部jdbc配置文件 --> <context:property-placeholder location="classpath:dbconfig.prope ...