热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

PAT1127.ZigZaggingonaTree(30)甲级

Supposethatallthekeysinabinarytreearedistinctpositiveintegers.Auniquebinar

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 (<&#61; 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

题目大意&#xff1a;给出一个树的中序和后序遍历结果&#xff0c;求它的Z字型层序遍历&#xff0c;也就是偶数层从右往左&#xff0c;奇数层从左往右遍历&#xff5e;
分析&#xff1a;分为3步&#xff1a;1.根据中序和后序建树 保存在tree二维数组中&#xff0c;比如&#xff1a;tree[i][0] &#61; val表示post[i]的左孩子是post[val]&#xff0c;tree[i][1] &#61; val表示post[i]的右孩子是post[val]&#xff5e;
2.进行广度优先搜索&#xff0c;将树从根结点开始所有结点层序遍历&#xff0c;保存在result二维数组中&#xff0c;比如&#xff1a;result[i]保存第i层所有结点的序列&#xff5e;
3.进行z字型输出&#xff0c;根据当前层号的奇偶性分别从左往右、从右往左遍历输出&#xff5e;

1. dfs&#xff1a;因为post(后序)是按照左、右、根的顺序遍历的&#xff0c;所以从右往左&#xff0c;最右边的肯定是根结点&#xff5e;所以postRight是当前子树的根结点的下标&#xff0c;将它的赋值给index&#xff0c;并继续dfs tree[index][0]和tree[index][1]&#xff5e;
根据post[postRight]的结点在in里面的下标位置i&#xff0c;可以得到i的左边是左子树&#xff0c;即inLeft 到 i - 1&#xff0c;右边是右子树&#xff1a;i &#43; 1 到 inRight。而对于post来说&#xff0c;根据左子树的结点个数i - inLeft可以得到[postLeft, postLeft &#43; (i - inLeft) - 1]是post中左子树的范围&#xff0c;[postLeft &#43; (i - inLeft), postRight - 1]是post中右子树的范围&#xff5e;
2.广度优先搜索&#xff0c;采用队列q&#xff0c;q中保存的是node结点&#xff0c;node.index表示当前节点在post中的下标&#xff0c;node.depth表示当前结点在树中的层数&#xff5e;

3.当 i % 2 &#61;&#61; 0的时候倒序输出&#xff0c;否则正序输出&#xff5e;

#include
#include
#include
using namespace std;
vector in, post, result[35];
int n, tree[35][2], root;
struct node {
int index, depth;
};
void dfs(int &index, int inLeft, int inRight, int postLeft, int postRight) {
if (inLeft > inRight) return;
index &#61; postRight;
int i &#61; 0;
while (in[i] !&#61; post[postRight]) i&#43;&#43;;
dfs(tree[index][0], inLeft, i - 1, postLeft, postLeft &#43; (i - inLeft) - 1);
dfs(tree[index][1], i &#43; 1, inRight, postLeft &#43; (i - inLeft), postRight - 1);
}
void bfs() {
queue q;
q.push(node{root, 0});
while (!q.empty()) {
node temp &#61; q.front();
q.pop();
result[temp.depth].push_back(post[temp.index]);
if (tree[temp.index][0] !&#61; 0)
q.push(node{tree[temp.index][0], temp.depth &#43; 1});
if (tree[temp.index][1] !&#61; 0)
q.push(node{tree[temp.index][1], temp.depth &#43; 1});
}
}
int main() {
cin >> n;
in.resize(n &#43; 1), post.resize(n &#43; 1);
for (int i &#61; 1; i <&#61; n; i&#43;&#43;) cin >> in[i];
for (int i &#61; 1; i <&#61; n; i&#43;&#43;) cin >> post[i];
dfs(root, 1, n, 1, n);
bfs();
printf("%d", result[0][0]);
for (int i &#61; 1; i <35; i&#43;&#43;) {
if (i % 2 &#61;&#61; 1) {
for (int j &#61; 0; j printf(" %d", result[i][j]);
} else {
for (int j &#61; result[i].size() - 1; j >&#61; 0; j--)
printf(" %d", result[i][j]);
}
}
return 0;
}







推荐阅读
  • 李逍遥寻找仙药的迷阵之旅
    本文讲述了少年李逍遥为了救治婶婶的病情,前往仙灵岛寻找仙药的故事。他需要穿越一个由M×N个方格组成的迷阵,有些方格内有怪物,有些方格是安全的。李逍遥需要避开有怪物的方格,并经过最少的方格,找到仙药。在寻找的过程中,他还会遇到神秘人物。本文提供了一个迷阵样例及李逍遥找到仙药的路线。 ... [详细]
  • 本文介绍了解决二叉树层序创建问题的方法。通过使用队列结构体和二叉树结构体,实现了入队和出队操作,并提供了判断队列是否为空的函数。详细介绍了解决该问题的步骤和流程。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 重入锁(ReentrantLock)学习及实现原理
    本文介绍了重入锁(ReentrantLock)的学习及实现原理。在学习synchronized的基础上,重入锁提供了更多的灵活性和功能。文章详细介绍了重入锁的特性、使用方法和实现原理,并提供了类图和测试代码供读者参考。重入锁支持重入和公平与非公平两种实现方式,通过对比和分析,读者可以更好地理解和应用重入锁。 ... [详细]
  • 本文介绍了Codeforces Round #321 (Div. 2)比赛中的问题Kefa and Dishes,通过状压和spfa算法解决了这个问题。给定一个有向图,求在不超过m步的情况下,能获得的最大权值和。点不能重复走。文章详细介绍了问题的题意、解题思路和代码实现。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文介绍了C++中省略号类型和参数个数不确定函数参数的使用方法,并提供了一个范例。通过宏定义的方式,可以方便地处理不定参数的情况。文章中给出了具体的代码实现,并对代码进行了解释和说明。这对于需要处理不定参数的情况的程序员来说,是一个很有用的参考资料。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文介绍了一种划分和计数油田地块的方法。根据给定的条件,通过遍历和DFS算法,将符合条件的地块标记为不符合条件的地块,并进行计数。同时,还介绍了如何判断点是否在给定范围内的方法。 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • 006_Redis的List数据类型
    1.List类型是一个链表结构的集合,主要功能有push,pop,获取元素等。List类型是一个双端链表的结构,我们可以通过相关操作进行集合的头部或者尾部添加删除元素,List的设 ... [详细]
  • Redis底层数据结构之压缩列表的介绍及实现原理
    本文介绍了Redis底层数据结构之压缩列表的概念、实现原理以及使用场景。压缩列表是Redis为了节约内存而开发的一种顺序数据结构,由特殊编码的连续内存块组成。文章详细解释了压缩列表的构成和各个属性的含义,以及如何通过指针来计算表尾节点的地址。压缩列表适用于列表键和哈希键中只包含少量小整数值和短字符串的情况。通过使用压缩列表,可以有效减少内存占用,提升Redis的性能。 ... [详细]
author-avatar
Dfsk刘海_368
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有