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 <stdio.h>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=;
int pos[maxn],in[maxn];
int n,m;
vector<int> res[];
struct node{
int data;
node* left,*right;
int lvl;
};
node* create(int inl,int inr,int posl,int posr){
if(inl > inr) return NULL;
node* root = new node;
root->data = pos[posr];
int k;
for(k=inl;k<=inr;k++){
if(pos[posr]==in[k]) break;
}
int numleft=k-inl;
root->left = create(inl,k-,posl,posl+numleft-);
root->right = create(k+,inr,posl+numleft,posr-);
return root;
}
void pr(node* root){
queue<node*> q;
root->lvl=;
q.push(root);
while(!q.empty()){
node* now=q.front();
q.pop();
res[now->lvl].push_back(now->data);
if(now->left!=NULL){
now->left->lvl=now->lvl+;
q.push(now->left);
}
if(now->right!=NULL){
now->right->lvl=now->lvl+;
q.push(now->right);
}
}
}
int main(){
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d",&in[i]);
}
for(int i=;i<n;i++){
scanf("%d",&pos[i]);
}
node* root = create(,n-,,n-);
pr(root);
int cnt=;
for(int i=;i<;i++){
if(i%==){
for(int j=res[i].size()-;j>=;j--){
printf("%d",res[i][j]);
cnt++;
if(cnt<n)printf(" ");
}
}
else{
for(int j=;j<res[i].size();j++){
printf("%d",res[i][j]);
cnt++;
if(cnt<n)printf(" ");
}
}
if(cnt==n)break;
}
}

注意点:最简单的一道30分题了。只要会建树,再层序遍历,把每层的数据保存起来,再根据层数正着输出或是反向输出就ac了。

ps:也可以用deque双向队列更方便的实现。每层的输出其实可以不用开vector数组,一层遍历完后输出就好了。

PAT A1127 ZigZagging on a Tree (30 分)——二叉树,建树,层序遍历的更多相关文章

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

  2. PAT甲级:1064 Complete Binary Search Tree (30分)

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

  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. PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)

    1064 Complete Binary Search Tree (30 分)   A Binary Search Tree (BST) is recursively defined as a bin ...

  6. PTA 04-树6 Complete Binary Search Tree (30分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/669 5-7 Complete Binary Search Tree   (30分) A ...

  7. PAT-2019年冬季考试-甲级 7-4 Cartesian Tree (30分)(最小堆的中序遍历求层序遍历,递归建树bfs层序)

    7-4 Cartesian Tree (30分)   A Cartesian tree is a binary tree constructed from a sequence of distinct ...

  8. Leetcode 102. Binary Tree Level Order Traversal(二叉树的层序遍历)

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  9. LeetCode:144_Binary Tree Preorder Traversal | 二叉树的前序遍历 | Medium

    题目:Binary Tree Preorder Traversal 二叉树的前序遍历,同样使用栈来解,代码如下: struct TreeNode { int val; TreeNode* left; ...

随机推荐

  1. 关于Unsupported major.minor version 52.0解决办法(再次回顾)

    对于web项目的配置问题,在很大程度上,tomcat的版本问题起到很大的决定性因素,例如以上问题:Unsupported major.minor version 52.0 表示stanford par ...

  2. Flask的Context(上下文)学习笔记

    上下文是一种属性的有序序列,为驻留在环境内的对象定义环境.在对象的激活过程中创建上下文,对象被配置为要求某些自动服务,如同步.事务.实时激活.安全性等等. 比如在计算机中,相对于进程而言,上下文就是进 ...

  3. linux学习笔记-conky配置开机启动方法

    我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 一.常用桌面的配置方法 创建启动文件并加入以下配置 ~/.config/autostart/conky.desktop [Des ...

  4. 新浪微博POI点签到数据及可视化的初步成果

    目前仅对山东省区域进行了抓取,权限不够高,抓取的速度非常慢,所以导致效率比较低... 数据抓取采用调用微博开放平台API的方法,数据存储采用mysql,格点数据分辨率为30″,山东省的MBR范围内(包 ...

  5. 【转】PHP 杂谈 坑爹的file_exists

    转自:http://www.cnblogs.com/baochuan/archive/2012/05/06/2445822.html 介绍   我发现了一个问题,今天与大家分享.我把整个过程描述一下. ...

  6. loadrunner 场景设计-设计与实践

    场景设计-设计与实践 by:授客 QQ:1033553122 以lr 11.0 自带Web Tours为例,进行以下测试 说明:以下测试仅供演示,学习设计思路 A.确定系统组件 简单B/S架构:Cli ...

  7. 《ASP.NET MVC企业实战》(三)MVC开发前奏

    ​ 在上一篇“<ASP.NET MVC企业级实战>(二)MVC开发前奏”中跟随作者大概了解了一些C#3.0和3.5中的新特性.本篇继续以这样的方式来学习C#中的一些特性.   一.C#3. ...

  8. (后端)出现org.hibernate.NonUniqueResultException的原因即解决办法

    百度出来的资料解决问题: 一个问题,事务处理完毕以后页面没有刷新.刚开始以为是前台js的问题,后来检查js没有任何问题,跟踪到后台发现后台报错了,错误信息如下:org.hibernate.NonUni ...

  9. 工作中常用到的Vim命令

    最近工作中需要到linux服务器上更改文件,苦于对vim的各种命令不熟悉,今天特此总结并熟悉一下各种vim命令,好提高工作效率.后期持续更新 vim编辑器个人设置 先复制一份vim配置模板到个人目录下 ...

  10. npm与yarn常用命令对比

    最近在用yarn,但是命令老是记不住,在此记录,方便日后翻看 图片截取自:http://yuanhehe.cn/2017/06/11/npm-%E4%B8%8E-Yarn-%E5%B8%B8%E7%9 ...