Source:

PAT A1127 ZigZagging on a Tree (30 分)

Description:

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

Keys:

Attention:

  • 开始方向弄反了-,-

Code:

  1. /*
  2. Data: 2019-05-31 21:17:07
  3. Problem: PAT_A1127#ZigZagging on a Tree
  4. AC: 33:50
  5.  
  6. 题目大意:
  7. 假设树的键值为不同的正整数;
  8. 给出二叉树的中序和后序遍历,输出二叉树的“之字形”层次遍历;
  9.  
  10. 基本思路:
  11. 建树,层次遍历,依次用队列和堆存储结点值,输出即可
  12. */
  13.  
  14. #include<cstdio>
  15. #include<stack>
  16. #include<queue>
  17. using namespace std;
  18. const int M=;
  19. int in[M],post[M];
  20. struct node
  21. {
  22. int data,layer;
  23. node *lchild,*rchild;
  24. };
  25.  
  26. node *Create(int postL, int postR, int inL, int inR)
  27. {
  28. if(postL > postR)
  29. return NULL;
  30. node *root = new node;
  31. root->data = post[postR];
  32. int k;
  33. for(k=inL; k<=inR; k++)
  34. if(in[k]==root->data)
  35. break;
  36. int numLeft = k-inL;
  37. root->lchild = Create(postL, postL+numLeft-, inL,k-);
  38. root->rchild = Create(postL+numLeft, postR-, k+,inR);
  39. return root;
  40. }
  41.  
  42. void Travel(node *root)
  43. {
  44. queue<node*> q;
  45. stack<node*> s;
  46. root->layer=;
  47. q.push(root);
  48. while(!q.empty())
  49. {
  50. root = q.front();q.pop();
  51. if(root->layer%==)
  52. {
  53. while(!s.empty())
  54. {
  55. printf(" %d", s.top()->data);
  56. s.pop();
  57. }
  58. printf(" %d", root->data);
  59. }
  60. else{
  61. if(root->layer==)
  62. printf("%d", root->data);
  63. else
  64. s.push(root);
  65. }
  66. if(root->lchild){
  67. root->lchild->layer=root->layer+;
  68. q.push(root->lchild);
  69. }
  70. if(root->rchild){
  71. root->rchild->layer=root->layer+;
  72. q.push(root->rchild);
  73. }
  74. }
  75. while(!s.empty())
  76. {
  77. printf(" %d", s.top()->data);
  78. s.pop();
  79. }
  80. }
  81.  
  82. int main()
  83. {
  84. #ifdef ONLINE_JUDGE
  85. #else
  86. freopen("Test.txt", "r", stdin);
  87. #endif
  88.  
  89. int n;
  90. scanf("%d", &n);
  91. for(int i=; i<n; i++)
  92. scanf("%d", &in[i]);
  93. for(int i=; i<n; i++)
  94. scanf("%d", &post[i]);
  95. node *root = Create(,n-,,n-);
  96. Travel(root);
  97.  
  98. return ;
  99. }

PAT_A1127#ZigZagging on a Tree的更多相关文章

  1. PAT1127:ZigZagging on a Tree

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

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

  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

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

  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 (30)

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

  7. A1127. ZigZagging on a Tree

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

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

  9. PAT 甲级 1127 ZigZagging on a Tree

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

随机推荐

  1. CF #324 DIV2 E题

    这题很简单,把目标位置排序,把目标位置在当前位置前面的往前交换,每次都是贪心选择第一个满足这样要求的数字. #include <iostream> #include <cstdio& ...

  2. 瀑布流 ajax 预载入 json

    pbl.json[模拟后台json数据]: [     {         "id": "511895",         "title": ...

  3. Bag标签之删除书包中的一条数据

    删除书包中的一条数据 查询 <esql module=help id=list> Select ID,Subject,Writer,DayTime From Messages </e ...

  4. scikit-learn:通过Non-negative matrix factorization (NMF or NNMF)实现LSA(隐含语义分析)

    之前写过两篇文章.各自是 1)矩阵分解的综述:scikit-learn:2.5.矩阵因子分解问题 2)关于TruncatedSVD的简介:scikit-learn:通过TruncatedSVD实现LS ...

  5. 刚開始学习的人非常有用之chm结尾的參考手冊打开后无法正常显示

    从网上下载了struts2的參考手冊.chm(本文适用全部已.chm结尾的文件)不能正常打开使用. 如图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/ ...

  6. 64位oracle数据库用32位plsql developer无法连接问题(无法载入oci.dll)

    在64位操作系统下安装oracle数据库,新下载了64位数据库(假设是32位数据库安装在64位的操作系统上,无论是client还是server端.都不要去选择C:\Program Files (x86 ...

  7. C#操作INI文件(明天陪你看海)

    C#操作INI文件 在很多的程序中,我们都会看到有以.ini为后缀名的文件,这个文件可以很方便的对程序配置的一些信息进行设置和读取,比如说我们在做一个程序后台登陆的时候,需要自动登录或者是远程配置数据 ...

  8. sklearn中的数据预处理和特征工程

    小伙伴们大家好~o( ̄▽ ̄)ブ,沉寂了这么久我又出来啦,这次先不翻译优质的文章了,这次我们回到Python中的机器学习,看一下Sklearn中的数据预处理和特征工程,老规矩还是先强调一下我的开发环境是 ...

  9. [转]"RDLC"报表-参数传递及主从报表

    本文转自:http://www.cnblogs.com/yjmyzz/archive/2011/09/19/2180940.html 今天继续学习RDLC报表的“参数传递”及“主从报表” 一.先创建D ...

  10. protobuf 编译 java js文件详解

    首先下载protobuf.exe 下载地址:https://download.csdn.net/download/qq_34756156/10220137 MessageBody.proto synt ...