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. JavaSE 异常抛光解析

    异常 异常指的是程序中的不正常现象,一般异常都是由第三方数据的使用造成的.java中每种异常现象都会有一个对应的异常类.java对异常的处理方式就是终止程序.异常机制其实是为了帮助我们找到程序中的问题 ...

  2. js实现的玫瑰花

    <html> <head> <meta charset="utf-8"> <title>开心快乐每一天</title> ...

  3. 机器学习是万能的吗?AI落地有哪些先决条件?

    机器学习是万能的吗?AI落地有哪些先决条件? https://mp.weixin.qq.com/s/9rNY2YA3BMpoY8NQ_rVIjQ 1.引言 入门机器学习或从事其相关工作前,不知道你思考 ...

  4. Android 开发学习笔记

    1.Genymotion 解决虚拟镜像下载速度特别慢的问题 http://blog.csdn.net/qing666888/article/details/51622762 2.

  5. VXLAN技术在数据中心的应用

    1.VXLAN技术可以通过物理交换机实现,也可以通过服务器实现,这两种实现的后台反应的是CT技术,还是IT技术来主导数据中心虚拟网络的发展. 2.物理交换机实现的VXLAN受限于芯片支持的规则,一般情 ...

  6. 安卓开发_浅谈AsyncTask

    现在就来学习一下AsyncTask. 一.先介绍一下AsyncTask: 在开发Android移动客户端的时候往往要使用多线程来进行操作,我们通常会将耗时的操作放在单独的线程执行,避免其占用主线程而给 ...

  7. Android深入四大组件(八)广播的注册、发送和接收过程

    前言 我们接着来学习Android四大组件中的BroadcastReceiver,广播主要就是分为注册.接收和发送过程.建议阅读此文前请先阅读Android深入理解四大组件系列的文章,知识重复的部分, ...

  8. Django基础篇--用户权限管理和组管理

    Django作为一个成熟的python后台开发框架,为开发者提供了很多内置的功能,开发者只需要做一些配置就可以完成原生操作中比较复杂的代码编写.这些内置功能中其中一个比较强大的功能就是后台用户管理类. ...

  9. SQL 三范式

    第一范式:无重复的列,一列只能包含一个字段 第二范式:主键约束,一行只能被唯一标识 第三范式:非主键字段要严格依赖于主键字段

  10. win7系统开机后电脑桌面背景变黑的解决方法

    自从微软放弃了对win7系统的维护更新,一些BUG也就慢慢出现了,最近用户反映,开机后电脑桌面背景变黑,即使重新换了桌面背景,还是会出现这种情况.下面小编就来告诉大家怎样解决这一问题. 1.点击开始菜 ...