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 ...
随机推荐
- 对Linux0.11 中 进程0 和 进程1分析
1. 背景 进程的创建过程无疑是最重要的操作系统处理过程之一,很多书和教材上说的最多的还是一些原理的部分,忽略了很多细节.比如,子进程复制父进程所拥有的资源,或者子进程和父进程共享相同的物理页面,拥有 ...
- tomcat整合apache
历时4个多小时,终于把tomcat与apache整合起来了. 中间出了各种各样的问题,现记录一下,也希望能对后来者有点帮助. 背景 apache与tomcat的区别联系大家都知道: tomcat能处理 ...
- mysql 备份和恢复的两条命令
压缩备份: 1.mysqldump -h localhost -u root -p dbname | gzip > dbname.sql.gz 压缩恢复: 1.gunzip < dbnam ...
- 《java入门第一季》之面向对象面试题(面向对象都做了哪些事情)
创建对象内存图解.
- SpriteBuilder中关于大量CCB文件的数字命名建议
开发者总是频繁的填充文件名字使用额外的0,以此来对抗长久以来的长痘:数字排序.如果你觉得在数字名字前添加额外的0是一个好主意,比如说Level0001,因为可能你会创建数以千记的关卡--请不要这样做! ...
- 加载SpriteBuilder中的scene为何不能带后缀
我们在Xcode中切换SpriteBuilder中的scene时,一般使用的是如下代码: -(void)exitButtonPressed:(CCControl*)sender{ CCLOG(@&qu ...
- Linux - test测试标志的意思总结
测试的标志 代表意义 1. 关於某个档名的『文件类型』判断,如 test -e filename 表示存在否 -e 该『档名』是否存在?(常用) -f 该『档名』是否存在且为文件(file)?(常用) ...
- myBatis源码学习之SqlSessionFactory
上一篇博客 SqlSessionFactoryBuilder 中介绍了它的作用就是获得DefaultSqlSessionFactory SqlSessionFactory是一个接口,其具体实现类是De ...
- os X下mds_stores占用大量cpu的解决办法
有时候发现, MacOS中, 有个叫做mds_stores的进程占了好多CPU, 于是要阻止这个行为, 据说这是MacOS在建索引然后只要把这个索引的关掉就好了 sh-3.2# mdutil -a - ...
- Java的运行原理
在Java中引入了虚拟机的概念,即在机器和编译程序之间加入了一层抽象的虚拟的机器.这台虚拟的机器在任何平台上都提供给编译程序一个的共同的接口.编译程序只需要面向虚拟机,生成虚拟机能够理解的代码,然后由 ...