title: PAT(A) 1127. ZigZagging on a Tree (30)
tags: PAT
categories: PAT甲级
date: 2018-03-13 14:21:14
description:
updated:
comments:
permalink:

原题目:

原题链接:https://www.patest.cn/contests/pat-a-practise/1127

1127. ZigZagging on a Tree (30)


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

题目大意

根据中序遍历和后序遍历建立一棵二叉树,然后拉链式输出,可以看到的是,奇数层由左向右输出,偶数层由右向左。

解题报告

  1. 建树
  2. 找出后序中最后一个元素在中序的位置,由这个位置将中序和后序一分为二,即两个序列左右部分元素个数相同。
  3. 以后序最后一个元素为根,将分开的两部分依次按同样方法建立左右子树。
  4. 输出,区分每一层的元素个数,判断向左还是向右,存入栈中,当一层进行完,再由栈进入队列中,保证顺序正确。

对于输出,也可以,先层序遍历,把每一层的元素分别保存下来,再根据是奇数层还是偶数层决定顺序输出还是倒序输出。

代码

/*
* Problem: 1127. ZigZagging on a Tree (30)
* Author: HQ
* Time: 2018-03-13
* State: Done
* Memo: 队列 栈 建树
*/
#include "iostream"
#include "queue"
#include "stack"
#include "vector"
using namespace std;struct Node {int data = 0;struct Node * left = NULL;struct Node * right = NULL;
};int N;
vector<int> inorder, postorder;int findPos(int x,int s,int l) {for (int i = 0; i < l; i++) {if (inorder[s + i] == x)return s + i;}return -1;
}struct Node * makeTree(int s1, int s2, int n) {struct Node *root = NULL;int x = postorder[s2 + n - 1];int pos = findPos(x, s1, n);if (pos != -1) {root = new struct Node;root->data = x;if (pos - s1 > 0)root->left = makeTree(s1, s2, pos - s1);if (s1 + n - pos - 1> 0)root->right = makeTree(pos + 1, s2 + pos - s1, s1 + n - pos - 1);}return root;
}void levelOrder(struct Node * root) {queue<struct Node *> q;stack<struct Node *> s;struct Node * temp;q.push(root);bool first = true;bool left = false;int cnt = 0, num = 1, tempnum = 0;while (!q.empty()) {temp = q.front();q.pop();if (first) {cout << temp->data;first = false;}elsecout << " " << temp->data;if (left) {if (temp->left != NULL) {s.push(temp->left);tempnum++;}if (temp->right != NULL) {s.push(temp->right);tempnum++;}}else {if (temp->right != NULL) {s.push(temp->right);tempnum++;}if (temp->left != NULL) {s.push(temp->left);tempnum++;}}cnt++;if (cnt == num) {num = tempnum;tempnum = 0;cnt = 0;left = !left;while (!s.empty()) {q.push(s.top());s.pop();}}}
}int main() {cin >> N;inorder.resize(N);postorder.resize(N);for (int i = 0; i < N; i++)cin >> inorder[i];for (int i = 0; i < N; i++)cin >> postorder[i];struct Node * root;root = makeTree(0, 0, N);levelOrder(root);cout << endl;system("pause");
}

PAT(A) 1127. ZigZagging on a Tree (30)相关推荐

  1. PAT甲级1127 ZigZagging on a Tree (30分):[C++题解]之字形层次遍历树bfs实现一层一层读入

    文章目录 题目分析 题目链接 题目分析 分析 给定中序遍历和后序遍历,先建树: 后面就是写一个bfs,层序遍历稍微改改即可. bfs要做的修改是如何一层一层的读入.令根结点是第一层,本题的之字形遍历就 ...

  2. 1127 ZigZagging on a Tree (30 分)【难度: 一般 / 知识点: 根据中序遍历 后序遍历建树】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805349394006016 PAT甲级中算是很传统的题了,就是建树,然后 ...

  3. 1127. ZigZagging on a Tree (30)

    很简单的题,先根据中序排序,与后序排序递归建树,然后层次遍历存入到out数组中,用now存储当前遍历的先后次序,用level来存储当前层数,date存储数据.用sort结构体排序,不同层次按从小到到排 ...

  4. PAT-1127. ZigZagging on a Tree (30)

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

  5. PAT甲级1064 Complete Binary Search Tree (30分):[C++题解]完全二叉搜索树BST

    文章目录 题目分析 题目链接 题目分析 思路: 第一步,构造含有n个结点的完全二叉树:第二步,将n个数值填入,使其满足二叉搜索树的性质. 对于第一步: 完全二叉树用一维数组可以存下,不过从根结点的下标 ...

  6. PAT (Advanced Level) Practice - 1127 ZigZagging on a Tree(30 分)

    题目链接:点击打开链接 题目大意:给出中序和后序序列,按照"S"形层序遍历输出. 解题思路:找规律可以发现,只要每次用queue保存,然后遍历了该层的所有结点后,把queue里的结 ...

  7. PAT甲级1151 LCA in a Binary Tree (30 分):[C++题解]LCA、最低公共祖先、哈希表映射

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 和下面这道题几乎是同一题:PAT甲级1143 Lowest Common Ancestor (30 分):[C++题解]LCA.最低 ...

  8. PAT甲级1135 Is It A Red-Black Tree (30分):[C++题解]判断红黑树

    文章目录 题目分析 题目链接 题目分析 分析 给定前序遍历,同时红黑树一定是二叉搜索树,所以它的中序遍历就是从小到大排序.因此这道题是给定了前序遍历和中序遍历让建立二叉搜索树(BST). 数据结构中有 ...

  9. PAT甲级1123 Is It a Complete AVL Tree (30分):[C++题解]建立平衡树、bfs,判完全二叉树

    文章目录 题目分析 题目链接 题目分析 来源:pat网站 本题作为进阶题,它的基础知识点如下几题. PAT甲级1066 Root of AVL Tree (25分):[C++题解]建立平衡树(AVL树 ...

最新文章

  1. javascript检测浏览器精简版
  2. 一个Python绘图示例程序中的几个语法糖果
  3. 17个提升iOS开发效率的神器
  4. Linux编程下open()函数的用法
  5. 深度优先搜索——单词方阵(洛谷 P1101)
  6. WdatePicker日期插件
  7. 拼多多开卖劳斯莱斯,直降122万,10万人表示想拼!
  8. 快排序和堆排序,最小堆、最大堆
  9. 【金猿技术展】PLC电力载波通信技术——电力系统特有通信方式
  10. BMP JPEG 图片转换为矢量图像 ContourTrace
  11. 未来精英论坛3.0 | 探秘桔厂科技和人文
  12. Visual studio 2017 安装
  13. InstallShield Professional,开发解决方案
  14. 【Adobe国际认证中文官网】Adobe中国摄影计划,免费安装 正版激活
  15. MiKTeX安装及添加到环境变量
  16. 照度稳定可调 LED 台灯(K 题)【高职高专组】-- 2021 年全国大学生电子设计竞赛
  17. C++为什么需要高精度计算
  18. Artifical Neural Networks
  19. 天大福利!世界第一科技出版公司 Springer 免费开放 400 多本电子书!
  20. 基于 P2P 技术的 Android 局域网内设备通信实践

热门文章

  1. PECL轻松安装PHP扩展
  2. 中国机器人产业图谱(2022)
  3. 在线 加密解密 工具
  4. Linux系统基本知识(4)
  5. JAVA实现雪花飘落动画效果
  6. (知乎)你是如何变的自律的
  7. java基础学习(五)
  8. 计算机系统基础 第三章 知识点梳理
  9. 机器学习 --基础入门介绍 他来啦!!!
  10. 二类电商运营怎么选品 二类电商怎么运营?