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<iostream>//根据中,后序建树,再层级遍历
#include<vector>
#include<queue>
using namespace std;
int n;
vector<int> post(30, 0), in(30, 0);
struct node{
int val;
node* left=NULL;
node* right=NULL;
node(int v):val(v), left(NULL), right(NULL){
}
};
node* buildtree(node* root, int inl, int inr, int postl, int postr){
if(inl>inr) return NULL;
root=new node(post[postr]);
int i=inl;
while(i<=inr&&in[i]!=post[postr]) i++;
int cnt=i-inl;
root->left=buildtree(root->left, inl, i-1, postl, postl+cnt-1);
root->right=buildtree(root->right, i+1, inr, postl+cnt, postr-1);
return root;
}
int main(){
cin>>n;
for(int i=0; i<n; i++)
cin>>in[i];
for(int i=0; i<n; i++)
cin>>post[i];
node* root=NULL;
root=buildtree(root, 0, n-1, 0, n-1);
queue<node*> q;
int flag=0, k=0, level=0;
q.push(root);
cout<<root->val;
while(!q.empty()){
vector<node*> v;
while(!q.empty()){
node *t=q.front();
q.pop();
if(t->left) v.push_back(t->left);
if(t->right) v.push_back(t->right);
}
for(int i = 0; i < v.size(); i++)
q.push(v[i]);
if(level % 2 == 0)
for(int i = 0; i < v.size(); i++)
printf(" %d", v[i]->val);
else
for(int i = v.size() - 1; i >= 0; i--)
printf(" %d", v[i]->val);
level ++;
}
return 0;
}

PAT 1127 ZigZagging on a Tree的更多相关文章

  1. 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 ...

  2. PAT甲级1127. ZigZagging on a Tree

    PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...

  3. PAT甲级 1127. ZigZagging on a Tree (30)

    1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  4. pat 甲级 1127. ZigZagging on a Tree (30)

    1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  5. 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 ...

  6. PAT 甲级 1127 ZigZagging on a Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805349394006016 Suppose that all the k ...

  7. PAT Advanced 1127 ZigZagging on a Tree (30) [中序后序建树,层序遍历]

    题目 Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree c ...

  8. PAT甲题题解-1127. ZigZagging on a Tree (30)-中序、后序建树

    根据中序遍历和前序遍历确定一棵二叉树,然后按“层次遍历”序列输出.输出规则:除根节点外,接下来每层的节点输出顺序是:先从左到右,再从右到左,交替输出 #include <iostream> ...

  9. 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 ...

随机推荐

  1. Python基础第二天

    一.内容 二.练习 练习1 题目:已知msg='hello knight 666'编写for循环,利用索引遍历出每一个字符 图示: 代码: msg = 'hello knight 666' msg_l ...

  2. 牛客OI周赛2-提高组

    A.游戏 链接:https://www.nowcoder.com/acm/contest/210/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语 ...

  3. 洛谷P3371 【模板】单源最短路径(弱化版)(SPFA解法)

    题目背景 本题测试数据为随机数据,在考试中可能会出现构造数据让SPFA不通过,如有需要请移步 P4779. 题目描述 如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 输入输出格式 输 ...

  4. ROS-URDF仿真

    前言:URDF (标准化机器人描述格式),是一种用于描述机器人及其部分结构.关节.自由度等的XML格式文件. 一.首先做一个带有四个轮子的机器人底座. 1.1 新建urdf文件 在chapter4_t ...

  5. oracle多语言环境下to_date时间转换问题

    现象:在多语言环境下使用过oracle的同学想必都遇到过这样一个问题, date_v date; date_v := to_date('2010/11/16');--或'2010/11/16' 同一个 ...

  6. Entityframework Code First 系列

    总篇, 下面会添加每个小篇的链接. 目录如下: 项目搭建 ……

  7. 为什么我的对象被 IntelliJ IDEA 悄悄的修改了?

    背景     最近,在复习JUC的时候调试了一把ConcurrentLinkedQueue的offer方法,意外的发现Idea在debug模式下竟然会 "自动修改" 已经创建的Ja ...

  8. 6.12---esultMap查询所有

  9. 前端面试题HTML

    浏览器页面有哪三层构成,分别是什么,作用是什么?

  10. 基于SOC方案的嵌入式开发-远程定时设备

    Soc方案实现简单的定时开关灯 http://club.gizwits.com/forum.php?mod=viewthread&tid=7787&highlight=%E5%AE%9 ...