PAT_A1127#ZigZagging on a Tree
Source:
Description:
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
Keys:
Attention:
- 开始方向弄反了-,-
Code:
/*
Data: 2019-05-31 21:17:07
Problem: PAT_A1127#ZigZagging on a Tree
AC: 33:50 题目大意:
假设树的键值为不同的正整数;
给出二叉树的中序和后序遍历,输出二叉树的“之字形”层次遍历; 基本思路:
建树,层次遍历,依次用队列和堆存储结点值,输出即可
*/ #include<cstdio>
#include<stack>
#include<queue>
using namespace std;
const int M=;
int in[M],post[M];
struct node
{
int data,layer;
node *lchild,*rchild;
}; node *Create(int postL, int postR, int inL, int inR)
{
if(postL > postR)
return NULL;
node *root = new node;
root->data = post[postR];
int k;
for(k=inL; k<=inR; k++)
if(in[k]==root->data)
break;
int numLeft = k-inL;
root->lchild = Create(postL, postL+numLeft-, inL,k-);
root->rchild = Create(postL+numLeft, postR-, k+,inR);
return root;
} void Travel(node *root)
{
queue<node*> q;
stack<node*> s;
root->layer=;
q.push(root);
while(!q.empty())
{
root = q.front();q.pop();
if(root->layer%==)
{
while(!s.empty())
{
printf(" %d", s.top()->data);
s.pop();
}
printf(" %d", root->data);
}
else{
if(root->layer==)
printf("%d", root->data);
else
s.push(root);
}
if(root->lchild){
root->lchild->layer=root->layer+;
q.push(root->lchild);
}
if(root->rchild){
root->rchild->layer=root->layer+;
q.push(root->rchild);
}
}
while(!s.empty())
{
printf(" %d", s.top()->data);
s.pop();
}
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif int n;
scanf("%d", &n);
for(int i=; i<n; i++)
scanf("%d", &in[i]);
for(int i=; i<n; i++)
scanf("%d", &post[i]);
node *root = Create(,n-,,n-);
Travel(root); return ;
}
PAT_A1127#ZigZagging on a Tree的更多相关文章
- PAT1127:ZigZagging on a Tree
1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- 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
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
- 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 ...
- 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 ...
随机推荐
- 查看OS 各项参数
查看CPU 在linux下 cat /proc/cpuinfo 可以得到CPU信息. 要注意的是CPU型号有不同的种类比如AMD Intel.可能在这个文件中显示的信息也不同.但终归是存在这个文件中的 ...
- SSH框架之Struts(3)——Struts的执行流程之核心方法
上篇讲了Tomcat实例化一个单例的ActionServlet.依据web.xml配置文件做好对应的初始化工作. 这时client产生一个.do结尾的request请求,採用get/post方式提交之 ...
- ustc 1117
无根树同构 有两种方法,一种是确定其中一棵树,另一棵树枚举根节点. 一种是,利用拓扑排序,先确定其中一棵树.另一棵树,若拓扑后剩两个节点,则枚举这两个节点为根结点,否则,只需做一次.注意,无根树节点入 ...
- [RxJS] AsyncSubject and ReplaySubject - Learn the Differences
We can use Subject as Observable and Observer: // Subject should be only used to emit value for priv ...
- [Mini Program] 尺寸单位 rpx
So each phone's width is 750rpx. And according to the device ratio (width:height), we can calucalate ...
- yum install tomcat
安装tomcat6 yum install tomcat6 tomcat6-webapps tomcat6-admin-webapps 启动tomcat6 service tomcat6 start ...
- linux中的alsa工具与Android中的tinyalsa工具【转】
本文转载自:http://blog.csdn.net/luckywang1103/article/details/48053015 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?) ...
- Single document interface和Multiple document interface
https://en.wikipedia.org/wiki/Single_document_interface https://msdn.microsoft.com/en-us/library/b2k ...
- KNN in c++
Pseudo Code of KNN We can implement a KNN model by following the below steps: Load the data Initiali ...
- nodejs windows环境安装
相信对于很多关注javascript发展的同学来说,nodejs已经不是一个陌生的词眼.有关nodejs的相关资料网上已经铺天盖地.由于它的高并发特性,造就了其特殊的应用地位. 国内目前关注最高,维护 ...