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 ...
随机推荐
- Kettle 变量(arg位置参数)
1.表输入中使用?占位作为kettle转换变量 数据预览: 获取变量数据: 使用?传入变量 需要勾选替换sql语句中的变量,并选则从步骤插入数据中所在步骤 数据预览
- 【转】MySQL sql_mode 说明(及处理一起 sql_mode 引发的问题)
1. MySQL 莫名变成了 Strict SQL Mode 最近测试组那边反应数据库部分写入失败,app层提示是插入成功,但表里面里面没有产生数据,而两个写入操作的另外一个表有数据.因为 inser ...
- elasticsearch概念及倒排索引简单介绍
一.概念 集群:一个或者多个节点组织在一起 节点:一个节点是集群中的一个服务器,由一个名字来标识,默认是一个随机的漫威角色名字. 分片:将索引划分为多份的能力,允许水平分割和扩展容量,多个分片相应请求 ...
- python 列表、元组、字典
一.列表 [ ] 如下的列子都可以成为列表,c=[1,2,3,4,5,6],d=["abc", "张三",“李四”],e=[1,2,3,"abc&qu ...
- Redis 禁用FLUSHALL FLUSHDB KEYS 命令
(error) ERR unknown command 'keys'问题解决(error) ERR unknown command 'FLUSHDB' 问题解决 背景 FLUSHALL FLUSH ...
- MySQL创建远程用户并授权
今天需要在本地测试系统功能,因为本地没有数据库,就需要在程序里面连接远程数据库: 先用ssh登录远程服务器,用root连上数据库看看情况: mysql> select Host,User,Pas ...
- [BZOJ 2743] [HEOI 2012] 采花
Description 萧芸斓是Z国的公主,平时的一大爱好是采花.今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花.花园足够大,容纳了 \(n\) 朵花,花有 \(c\) 种颜色(用整数 \ ...
- BZOJ4177Mike的农场——最小割
题目描述 Mike有一个农场,这个农场n个牲畜围栏,现在他想在每个牲畜围栏中养一只动物,每只动物可以是牛或羊,并且每个牲畜围栏中的饲养条件都不同,其中第i个牲畜围栏中的动物长大后,每只牛可以卖a[i] ...
- POJ 3580-SuperMemo-splay树
很完整的splay操作.做了这题就可以当板子用了. #include <cstdio> #include <algorithm> #include <cstring> ...
- 前端 -- HTML内容
HTML介绍 Wed服务本质 import socket sk = socket.socket() sk.bind(("127.0.0.1", 8080)) sk.listen(5 ...