1127 ZigZagging on a Tree (30 分)

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:

  1. 8
  2. 12 11 20 17 1 15 8 5
  3. 12 20 17 11 15 8 5 1

Sample Output:

  1. 1 11 5 8 17 12 20 15

题意:中序和后序建树,然后按zigzagging order输出。

分析:层序遍历的时候将节点输出到容器中,最后输出的时候根据奇数还是偶数来输出结点

  1. /**
  2. * Copyright(c)
  3. * All rights reserved.
  4. * Author : Mered1th
  5. * Date : 2019-02-28-14.24.50
  6. * Description : A1127
  7. */
  8. #include<cstdio>
  9. #include<cstring>
  10. #include<iostream>
  11. #include<cmath>
  12. #include<algorithm>
  13. #include<string>
  14. #include<unordered_set>
  15. #include<map>
  16. #include<vector>
  17. #include<set>
  18. #include<queue>
  19. using namespace std;
  20. ;
  21. int n,post[maxn],in[maxn];
  22. vector<int> ans[maxn];
  23. struct Node{
  24. int val,layer;
  25. Node* lchild;
  26. Node* rchild;
  27. Node(int _val,int _layer){
  28. val=_val;
  29. lchild=NULL;
  30. rchild=NULL;
  31. layer=_layer;
  32. }
  33. };
  34. ;
  35. Node* create(int inL,int inR,int postL,int postR,int layer){
  36. if(inL>inR) return NULL;
  37. if(layer>maxlayer) maxlayer=layer;
  38. int rootVal=post[postR];
  39. Node* root=new Node(rootVal,layer);
  40. int k;
  41. for(int i=inL;i<=inR;i++){
  42. if(post[postR]==in[i]){
  43. k=i;
  44. break;
  45. }
  46. }
  47. int numLeft=k-inL;
  48. root->lchild=create(inL,k-,postL,postL+numLeft-,layer+);
  49. root->rchild=create(k+,inR,postL+numLeft,postR-,layer+);
  50. return root;
  51. }
  52.  
  53. void BFS(Node* root){
  54. queue<Node*> q;
  55. q.push(root);
  56. while(!q.empty()){
  57. Node* now=q.front();
  58. q.pop();
  59. ans[now->layer].push_back(now->val);
  60. if(now->lchild!=NULL) q.push(now->lchild);
  61. if(now->rchild!=NULL) q.push(now->rchild);
  62. }
  63. }
  64.  
  65. int main(){
  66. #ifdef ONLINE_JUDGE
  67. #else
  68. freopen("1.txt", "r", stdin);
  69. #endif
  70. scanf("%d",&n);
  71. ;i<n;i++){
  72. scanf("%d",&in[i]);
  73. }
  74. ;i<n;i++){
  75. scanf("%d",&post[i]);
  76. }
  77. Node* root=create(,n-,,n-,);
  78. BFS(root);
  79. ;i<=maxlayer;i++){
  80. ){
  81. printf(]);
  82. continue;
  83. }
  84. ==){
  85. ;j<ans[i].size();j++){
  86. printf(" %d",ans[i][j]);
  87. }
  88. }
  89. else{
  90. ;j>=;j--){
  91. printf(" %d",ans[i][j]);
  92. }
  93. }
  94. }
  95. ;
  96. }

1127 ZigZagging on a Tree (30 分)的更多相关文章

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

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

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

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

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

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

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

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

  6. PAT甲级1127. ZigZagging on a Tree

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

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

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

  9. PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)

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

随机推荐

  1. 【湖南师范大学2018年大学生程序设计竞赛新生赛 L】【HDOJ2476】【区间DP】

    https://www.nowcoder.com/acm/contest/127/L L 小小粉刷匠 题目描述 "lalala,我是一个快乐的粉刷匠",小名一边快活地唱着歌,一边开 ...

  2. 【Jmeter】分布式并发测试

    一.前提: 1.最近在做一下压测,但是单台服务器的CPU,内存可能不够支撑压测的项目,这时候,我们可以使用Jmeter分布式压测. 2.本次使用的环境: 1台服务器做master(调度器) 5台服务器 ...

  3. Go Example--数组

    package main import "fmt" func main() { //定义一个数组并完成初始化,初始值为对应的零值 var a [5]int fmt.Println( ...

  4. mongodb添加验证用户 删除用户

    1.创建用户 db.createUser( { user:<name_string>,                   #字符串 pwd:<password_string> ...

  5. MySQL Innodb Engine -- 文件格式(innodb_file_format)

    ======================================================== 在InnoDB 1.0.x版本之前,InnoDB 存储引擎提供了 Compact 和 ...

  6. classLoader卸载与jvm热部署

    以下的相关介绍都是在未使用dcevm的情况 classLoader的卸载机制 jvm中没有提供class及classloader的unload方法.那热部署及osgi中是通过什么机制来实现的呢?实现思 ...

  7. hasura graphql-engine v1.0.0-alpha30 版本新功能介绍

    hasura graphql-engine v1.0.0-alpha30 发布了,以下为一些变动的简单说明 破坏性的变动 order_by 中的desc 从 desc nulls last 修改为 d ...

  8. 关于margin padding

    margin padding设置百分比是以父元素的宽度作参考. 定位的left,top等取百分比 则以参照定位元素的padding+width做参考 margin 四个同时设定 以margin-lef ...

  9. 【Hi3516】 uboot下烧写BSP

    setenv serverip xx.xx.xx.xx setenv ipaddr xx.xx.xx.xx setenv ethaddr xx:xx:xx:xx:xx:xx setenv netmas ...

  10. 记录:.user.ini 使用

    记录:.user.ini 使用 可以用于防跨站配置. .user.ini 注意安全问题 动态加载,默认 5 分钟自动刷新. php 5.3 以后的版本支持. 修改完成后再将文件锁定. 相关链接: 神秘 ...