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

  1. #include <stdio.h>
  2. #include <algorithm>
  3. #include <queue>
  4. using namespace std;
  5. const int maxn=;
  6. int pos[maxn],in[maxn];
  7. int n,m;
  8. vector<int> res[];
  9. struct node{
  10. int data;
  11. node* left,*right;
  12. int lvl;
  13. };
  14. node* create(int inl,int inr,int posl,int posr){
  15. if(inl > inr) return NULL;
  16. node* root = new node;
  17. root->data = pos[posr];
  18. int k;
  19. for(k=inl;k<=inr;k++){
  20. if(pos[posr]==in[k]) break;
  21. }
  22. int numleft=k-inl;
  23. root->left = create(inl,k-,posl,posl+numleft-);
  24. root->right = create(k+,inr,posl+numleft,posr-);
  25. return root;
  26. }
  27. void pr(node* root){
  28. queue<node*> q;
  29. root->lvl=;
  30. q.push(root);
  31. while(!q.empty()){
  32. node* now=q.front();
  33. q.pop();
  34. res[now->lvl].push_back(now->data);
  35. if(now->left!=NULL){
  36. now->left->lvl=now->lvl+;
  37. q.push(now->left);
  38. }
  39. if(now->right!=NULL){
  40. now->right->lvl=now->lvl+;
  41. q.push(now->right);
  42. }
  43. }
  44. }
  45. int main(){
  46. scanf("%d",&n);
  47. for(int i=;i<n;i++){
  48. scanf("%d",&in[i]);
  49. }
  50. for(int i=;i<n;i++){
  51. scanf("%d",&pos[i]);
  52. }
  53. node* root = create(,n-,,n-);
  54. pr(root);
  55. int cnt=;
  56. for(int i=;i<;i++){
  57. if(i%==){
  58. for(int j=res[i].size()-;j>=;j--){
  59. printf("%d",res[i][j]);
  60. cnt++;
  61. if(cnt<n)printf(" ");
  62. }
  63. }
  64. else{
  65. for(int j=;j<res[i].size();j++){
  66. printf("%d",res[i][j]);
  67. cnt++;
  68. if(cnt<n)printf(" ");
  69. }
  70. }
  71. if(cnt==n)break;
  72. }
  73. }

注意点:最简单的一道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. 漫画 | Servlet属于线程安全的吗?

    Servlet属于线程安全的吗? Servlet不是线程安全的 在JSP中,只有一行代码:<%=A+B %>,运行结果如何? jsp和servlet有什么关系? jsp一般被用在view层 ...

  2. vue中使用axios(异步请求)和mock.js 模拟虚假数据

    一.使用axios 1.安装 npm install --save axios 2.引用 import Axios from 'axios' Vue.prototype.Axios = Axios 二 ...

  3. 【工具相关】Web-HTML特殊字符对照表

    特殊符号 命名实体 十进制编码 特殊符号 命名实体 十进制编码 特殊符号 命名实体 十进制编码 Α Α Α Β Β Β Γ Γ Γ Δ Δ Δ Ε Ε Ε Ζ Ζ Ζ Η Η Η Θ Θ Θ Ι Ι ...

  4. 《Attention is All You Need》浅读(简介+代码)

    2017年中,有两篇类似同时也是笔者非常欣赏的论文,分别是FaceBook的<Convolutional Sequence to Sequence Learning>和Google的< ...

  5. 基于Grafana的监控数据钻取功能应用实践

    互联网企业中,随着机器规模以及业务量的爆发式增长,监控数据逐渐成为一种大数据,对监控大数据的分析,包括数据采集.数据缓存.数据聚合分析.数据存储.数据展现等几个阶段.不同阶段有不同的解决方案及支撑工具 ...

  6. MySQL四种隔离级别和MVCC

    事务在一个数据库中的地位尤为重要,尤其是高并发的场合.保证数据库操作的原子性和错误出现情况下的回滚,对数据的安全性和可靠性提供了保障.事务有四大原则,即ACID原则.网上关于这个问题的文章有很多,读者 ...

  7. [20171031]rman xxx Failure.txt

    [20171031]rman xxx Failure.txt --//简单测试 List Failure, Advise Failure and Repair Failure命令在11g下,也许以后工 ...

  8. python第七十九天--第十四周作业

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 监控.net 网站 Glimpse

    使用Nuget 安装Glimpse 安装好后,config会默认添加几个节点 安装好之后 只需要浏览器输入  网站/Glimpse.axd 再次进入网站 就可以查看(ajax sql session ...

  10. ssh无法访问服务器报“ssh-dss”认证错误

    故障描述: 在windows下的ssh客户端直接报错,内容为: Unable to negotiate with legacyhost: no matching host key type found ...