PAT1127:ZigZagging on a Tree
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 思路
类似Pat1029,根据中序遍历和后序遍历序列确定树,以层次遍历的形式存入vector<vector<int>>中,然后按每一层往返输出就行。 代码
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
vector<int> postorder(31);
vector<int> inorder(31);
vector<vector<int>> levels(31); void buildTree(const int pl,const int pr,const int il,const int ir,const int level)
{ if(pl > pr || il > ir)
return;
int root = postorder[pr],i = 0;
while( inorder[il + i ] != root) i++;
levels[level].push_back(root);
buildTree(pl,pl + i - 1,il,il + i - 1,level + 1);
buildTree(pl + i ,pr - 1,il + i + 1,ir,level + 1);
} void zigzag()
{
cout << levels[0][0];
bool zigzag = false;
for(int i = 1; i < levels.size() && !levels[i].empty();i++)
{
if(zigzag)
{
for(int j = levels[i].size() - 1;j >= 0;j--)
{
cout << " " << levels[i][j];
}
}
else
{
for(int j = 0;j < levels[i].size();j++)
{
cout << " " << levels[i][j];
}
}
zigzag = !zigzag;
}
} int main()
{
int N;
while(cin >> N)
{
for(int i = 0;i < N;i++)
cin >> inorder[i];
for(int i = 0;i < N;i++)
cin >> postorder[i];
buildTree(0,N - 1,0, N - 1,0);
zigzag();
}
}
PAT1127:ZigZagging on a Tree的更多相关文章
- PAT甲级1127. ZigZagging on a Tree
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
- 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 ...
- PAT甲级 1127. ZigZagging on a Tree (30)
1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- 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 ...
- pat 甲级 1127. ZigZagging on a Tree (30)
1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT_A1127#ZigZagging on a Tree
Source: PAT A1127 ZigZagging on a Tree (30 分) Description: Suppose that all the keys in a binary tre ...
- A1127. ZigZagging on a Tree
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...
- 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 ...
- PAT 甲级 1127 ZigZagging on a Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805349394006016 Suppose that all the k ...
随机推荐
- 在Cocos2D中改变动态物体为静态物体
原文链接,有压缩和简化 1.导入一个新的头文件 首先你要知道,不是所有Chimpunk特性都通过Cocos2d的类暴露出来,比如CCPhysicsNode和CCPhysicsBody.对于一些更高级的 ...
- STL - priority_queue(优先队列)
优先级队列priority_queue 最大值优先级队列.最小值优先级队列 优先级队列适配器 STL priority_queue 用来开发一些特殊的应用. priority_queue<int ...
- OC语言(三)
十九.一些规范 import系统自带的用尖括号<>来包含. 发现需求不清晰,一定要先搞明白才去做. 多文件开发,文件名和类名一致 命令行里的做法:(只是编译链接主文件,但是全部编译链接会出 ...
- 网站开发进阶(二十二)HTML UI知识汇总(更新中...)
HTML知识汇总(更新中...) 1.<iframe> 标签 浏览器支持 所有浏览器都支持 <iframe> 标签. 定义和用法 iframe 元素会创建包含另外一个文档的内联 ...
- LeetCode之“链表”:Reorder List
题目链接 题目要求: Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You ...
- Android性能优化典例(一)
在Android开发过程中,很多时候往往因为代码的不规范.api使用不恰当.控件的使用场景考虑不全面和用户不恰当的操作等都能引发一系列性能问题的,下面就是我目前整理的一些Android开发过程中需要注 ...
- 关于Service中bindService注意的几个问题
最近有用到Activity需要不断的从Service中获取数据,第一个想法肯定就是通过bind回调机制了,有几点概念模糊特此记录下: 单独使用bindService(),unbindService() ...
- GDI+ 读取jpg图片每个像素的值
// 读取jpg图像像素rgb值.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #in ...
- ROS探索总结(十五)——amcl(导航与定位)
在理解了move_base的基础上,我们开始机器人的定位与导航.gmaping包是用来生成地图的,需要使用实际的机器人获取激光或者深度数据,所以我们先在已有的地图上进行导航与定位的仿真. amcl是移 ...
- IP封包的封装 - 首部内容
IP 封包的封装 目前因特网社会的 IP 有两种版本,一种是目前使用最广泛的 IPv4 (Internet Protocol version 4, 因特网协定第四版), 一种则是预期未来会热门的 IP ...