A1127. ZigZagging on a Tree
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
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
typedef struct NODE{
struct NODE* lchild, *rchild;
int data;
int lev;
}node;
int post[], in[], N;
node* create(int inL, int inR, int postL, int postR, int level){
if(inL > inR){
return NULL;
}
node* root = new node;
root->data = post[postR];
root->lev = level;
int mid;
for(mid = inL; mid <= inR; mid++){
if(in[mid] == root->data)
break;
}
int len = mid - inL;
root->lchild = create(inL, mid - , postL, postL + len - , level + );
root->rchild = create(mid + , inR, postL + len, postR - , level + );
return root;
}
void levelOrder(node* root){
queue<node*> Q;
Q.push(root);
int nowLev = root->lev;
int reverTag = ;
int cnt = ;
vector<node*> vec;
while(Q.empty() == false){
node* temp = Q.front();
Q.pop();
if(temp->lev == nowLev){
vec.push_back(temp);
}else{
nowLev = temp->lev;
if(reverTag == ){
reverTag = ;
for(int i = vec.size() - ; i>= ; i--){
cnt++;
printf("%d ", vec[i]->data);
}
}else{
reverTag = ;
for(int i = ; i < vec.size(); i++){
cnt++;
printf("%d ", vec[i]->data);
}
}
vec.clear();
vec.push_back(temp);
}
if(temp->lchild != NULL)
Q.push(temp->lchild);
if(temp->rchild != NULL)
Q.push(temp->rchild);
}
if(reverTag == ){
for(int i = ; i < vec.size(); i++){
cnt++;
if(cnt == N)
printf("%d", vec[i]->data);
else printf("%d ", vec[i]->data);
}
}else{
for(int i = vec.size() - ; i >= ; i--){
cnt++;
if(cnt == N)
printf("%d", vec[i]->data);
else printf("%d ", vec[i]->data);
}
}
}
int main(){
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, );
levelOrder(root);
cin >> N;
return ;
}
总结:
1、题意:中序后序建树,再用层序输出,输出时一行逆序一行正序。
2、可以在建树时顺便将层次信息也加入节点。在层序遍历时使用一个vector,在当前层数相同时,仅仅把本该访问的节点存入vector,当层数发生改变时,输出vector内所有元素(设置一个计数器,如果上次是正序输出,则本次逆序输出); 或者正常层序遍历,再多用一个栈+一个队列,当需要正序输出该层时,节点入队列,需要逆序则节点入栈,层数改变时输出栈中或队列中全部节点,并清空。
A1127. ZigZagging on a Tree的更多相关文章
- 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 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甲级——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_A1127#ZigZagging on a Tree
Source: PAT A1127 ZigZagging on a Tree (30 分) Description: Suppose that all the keys in a binary tre ...
- 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 ...
- PAT1127:ZigZagging on a Tree
1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- 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 ...
随机推荐
- K3BOM跳层
A自制件,B自制件,C外购件 ,结构为A-B-C 如果需要跳层,则设置A-B跳层,B-C跳层,则生成A计划订单,C计划订单, 假设单独A-B跳层,则MRP运算出的结果也是A计划订单,B计划订单,C计划 ...
- Struts2——namespace、action、以及path问题
简单的介绍下Struts2中的几个简单的问题(namespace.action.以及path问题) namespace(命名空间) Namespace决定了action的访问路径,默认为“”,意味着可 ...
- CSS单元的位置和层次-div标签
我们都知道,在网页上利用HTML定位文字和图象是一件“令人心痛”的事情.我们必须使用表格标签和隐式GIF图象,即使这样也不能保证定位的精确,因为浏览器和操作平台的不同会使显示的结果发生变化. 而CSS ...
- python数学第九天【统计】
- 命名自我规约manual
前端: 所有文件命名都小写,多个单词连接使用 “-” 变量命名规则还是驼峰式,或者在前面加个 “_” SQL: MySQL: 所有命名都小写,无论库.表.还是字段等等,都小写 多个单词之间的分隔,使用 ...
- .NET提供了三种后台输出js的方式:
.NET提供了三种后台输出js的方式: 首先创建 js文件testjs.js { Page.ClientScript.RegisterClientScriptInclude("keys ...
- Linq:使用Take和Skip实现分页
Skip,Take: list = list.Skip(pageNum * pageSize).Take(pageSize).ToList(); pageSize :表示一页多少条. pageNum: ...
- window.onpopstate
概述 window.onpopstate是popstate事件在window对象上的事件句柄. 每当处于激活状态的历史记录条目发生变化时,popstate事件就会在对应window对象上触发. 如果当 ...
- python与java的内存机制不一样;java的方法会进入方法区直到对象消失 方法才会消失;python的方法是对象每次调用都会创建新的对象 内存地址都不i一样
python与java的内存机制不一样;java的方法会进入方法区直到对象消失 方法才会消失;python的方法是对象每次调用都会创建新的对象 内存地址都不i一样
- PlaNet,使用图像输入来学习世界模型
Google AI团队与DeepMind合作,上周宣布了一个名为PlaNet的新的开源“Deep Planning”网络. PlaNet是一个人工智能代理,它只使用图像输入来学习世界模型,并使用这些模 ...