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. 基于IDEA的bs三层架构

    1.在大学的老师讲课中,可能会用到myeclipse或者eclipse来进行编译运行.其中的缺点就是要自行去下载开发所需要的一些jar包,要考虑都版本的不同造成的影响,且ORACLE和MYSQL的链接 ...

  2. 7种html5css3网页图片展示特效代码

    鼠标拖拽图片渐变透明切换特效 mobile手机左右滑动切换幻灯片 游戏透明提示图层幻灯片特效 可以编辑滚动条灯片颜色的scroll插件 几种文字动画显示插件代码 360度背景图片旋转的css3动画 左 ...

  3. git命令详解( 三 )

    此篇为git命令的第三篇 目录 git Pull 模拟团队合作 Git Pull 在上一篇的结尾我们已经知道了如何用 git fetch 获取远程的数据, 现在我们学习如何将这些变化更新到我们的工作当 ...

  4. NODE获取节点删除空格的操作

    NODE节点操作有: object.parentNode:获取某子元素的父级: object.childNodes:是获取所有的子元素节点,返回数组类型: object.lastChild: 获取该元 ...

  5. loadrunner 场景设计-学习笔记之性能误区

    场景设计-学习笔记之性能误区 by:授客 QQ:1033553122 场景假设: 每个事务仅包含一次请求,执行10000个并发用户数 性能误区: 每秒并发用户数=每秒向服务器提交请求数 详细解答: 每 ...

  6. Sqlautocode使用过程的一些坑

    Sqlautocode是SQLAlchemy一个数据库映射工具,可以将数据库文件映射为python代码,直接在程序中移植使用.最近在使用过程中遇到了一些坑,通过用代码编辑工具pycharm阅读源码和多 ...

  7. JavaScript大杂烩18 - Web开发的MVVM模式

    MVC VS. MVP VS. MVVM  了解MVVM模式之前,我们先来简单了解一下从MVC到MVVM的变迁.这个变迁是耦合从紧到松的变迁,是对依赖处理的进化,是应对变化技术的成熟. MVC  MV ...

  8. Linux下mysql5.7数据库root登录的问题

    本文最后修改时间:20180313 root默认为空密码,默认远程无法登录. mysql5.7更新了user表,网上的方法试了很多,都有点问题 #先停止MySQL服务 $ sudo service m ...

  9. 简述 Spring Cloud 是什么1

    很多同学都了解了Spring ,了解了 Spring Boot, 但对于 Spring Cloud 是什么还是比较懵逼的. 本文带你简单的了解下,什么是Spring Cloud. Spring Clo ...

  10. 如何将 asp.net core 应用进行 docker 容器部署

    asp.net core 部署在 docker 容器中比较简单,但常因asp.net core程序发布的问题造成容器无法正常启动.现在把详细的操作的步骤记录如下: 一.asp.net core web ...