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. Rails中生成并在ActionMailer附件中发送csv、excel、pdf、zip文件

    1.修改Gemfile # PDF generator gem 'prawn' gem 'prawn-table' # Excel generator gem 'rubyXL' #Zip genera ...

  2. Codeforces #123D: 后缀数组+单调栈

    D. String     You are given a string s. Each pair of numbers l and r that fulfill the condition 1 ≤  ...

  3. SSM(Spring5.x+Mybatis3)框架搭建【解决日志问题】(Github源码)

    闲来无事,用SSM写个小东西,发现spring已经迭代到5.x了,遂出此文,希望对各位同学有些许帮助. IDE:idea OS:windows 源代码:https://github.com/JHeav ...

  4. 浅谈JVM编译原理->.java文件转变为.class文件的过程

    为什么需要编译? 我们平常写代码,有规范的命名方式,都能够看得懂,但是我们写的代码计算机是看不懂的,所以需要编译,也就是一个转换的过程,如下: 1.这个是咱们平时写的代码,就比较好理解,对人友好 2. ...

  5. ASP.NET <% %>的各种形式用法

    1.<% %>用来绑定后台代码 < % ;i<;i++) { Reaponse.Write(i.ToString()); } %> 2.<%# %> 是在绑定 ...

  6. c语言实现shell

    shell的编写 命令行传参数 每个C语言程序都必须有一个称为main()的函数,作为程序启动的起点.当执行程序时,命令行参数(command-line argument)(由shell逐一解析)通过 ...

  7. 20155224 实验一《Java开发环境的熟悉》实验报告

    实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版) ...

  8. 2016-2017-2 20155227实验三《敏捷开发与XP实践》实验报告

    2016-2017-2 20155227实验三<敏捷开发与XP实践>实验报告 实验内容 一.实验内容 XP基础 XP核心实践 相关工具 二.实验过程 (一)敏捷开发与XP 1.XP是以开发 ...

  9. 【转载】C/C++杂记:深入虚表结构

    原文:C/C++杂记:深入虚表结构 1. 虚表与“虚函数表” 在“C/C++杂记:虚函数的实现的基本原理”一文中曾提到“虚函数表”的概念,只是为了便于理解,事实是:虚函数表并不真的独立存在,它只是虚表 ...

  10. vmware因为软件出过一次复制的错误导致不能复制到主机的解决方法

    只需要把vmware的虚拟机进程全部结束掉,然后重置(先设置不勾选复制等,然后保存后在勾选上并保存)一次虚拟机隔离设置(需要在关闭虚拟机的情况下设置,否则就是灰色不允许操作),然后再开启虚拟机,就能正 ...