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

题目大意:给出二叉树的中根遍历和后根遍历序列,给出zigzag遍历序列,就是隔层从左到右,从右到左这样转换遍历。

//这个我当然是不会了,好久没做二叉树的题目了。

转自:https://www.liuchuo.net/archives/3758

  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include<cstdio>
  5. using namespace std;
  6. vector<int> in, post, result[];
  7. int n, tree[][], root;
  8. struct node {
  9. int index, depth;//保存index和深度。
  10. };
  11. //将二叉树的结构存储在了tree中。
  12. void dfs(int &index, int inLeft, int inRight, int postLeft, int postRight) {
  13. if (inLeft > inRight) return;
  14. index = postRight;//进来之后再赋值,真的厉害。
  15. int i = ;
  16. while (in[i] != post[postRight]) i++;
  17. dfs(tree[index][], inLeft, i - , postLeft, postLeft + (i - inLeft) - );
  18. dfs(tree[index][], i + , inRight, postLeft + (i - inLeft), postRight - );
  19. }
  20. //dfs函数得到的root实际上是post遍历中的下标。
  21. void bfs() {
  22. queue<node> q;
  23. q.push(node{root, });
  24. while (!q.empty()) {
  25. node temp = q.front();
  26. q.pop();
  27. result[temp.depth].push_back(post[temp.index]);
  28. if (tree[temp.index][] != )//左子树不为空。
  29. q.push(node{tree[temp.index][], temp.depth + });
  30. //那么此时push进的是左子树,并且深度+1.
  31. if (tree[temp.index][] != )//右子树不为空。
  32. q.push(node{tree[temp.index][], temp.depth + });
  33. }
  34. }
  35. int main() {
  36. cin >> n;
  37. in.resize(n + ), post.resize(n + );
  38. for (int i = ; i <= n; i++) cin >> in[i];//输入中序遍历
  39. for (int i = ; i <= n; i++) cin >> post[i];//输入后序遍历
  40. dfs(root, , n, , n);//将根存在了root中。
  41. bfs();
  42. printf("%d", result[][]);
  43. for (int i = ; i < ; i++) {
  44. if (i % == ) {
  45. for (int j = ; j < result[i].size(); j++)
  46. printf(" %d", result[i][j]);
  47. } else {
  48. for (int j = result[i].size() - ; j >= ; j--)
  49. printf(" %d", result[i][j]);
  50. }
  51. }
  52. return ;
  53. }

//柳神真厉害。

1.使用dfs在遍历的过程中传进去index引用参数,直接赋值,并且使用tree二维数组存储二叉树的结构

2.使用结构体node,存储下标和层数,下标是在post序列中可以寻到节点的。

3.对奇数层和偶数层,分别用不同的方式打印。

代码来自:https://www.cnblogs.com/chenxiwenruo/p/6506517.html

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <string.h>
  5. #include <string>
  6. #include <map>
  7. #define LEFT 0
  8. #define RIGHT 1
  9. using namespace std;
  10. const int maxn=;
  11. int inorder[maxn];
  12. int postorder[maxn];
  13. int level[maxn][maxn]; //每层的节点id
  14. int levelcnt[maxn]; //每层的节点个数
  15. int maxlayer=;
  16. int cnt=; //节点id
  17. struct Node{
  18. int left=-,right=-;
  19. int val;
  20. }node[maxn];
  21.  
  22. //根据中序遍历和后序遍历建立树
  23. void build(int inL,int inR,int postL,int postR,int fa,int LorR){
  24. if(inL>inR)
  25. return;
  26. int val=postorder[postR];
  27. int idx;
  28. //在中序遍历中找出父亲节点的索引,其左边是左子树,右边是右子树
  29. for(int i=inL;i<=inR;i++){
  30. if(inorder[i]==val){
  31. idx=i;
  32. break;
  33. }
  34. }
  35. int lnum=idx-inL;//左子树的节点个数
  36. cnt++;
  37. node[cnt].val=val;
  38. if(LorR==LEFT)
  39. node[fa].left=cnt;
  40. else if(LorR==RIGHT)
  41. node[fa].right=cnt;
  42. int tmp=cnt;
  43. build(inL,idx-,postL,postL+lnum-,tmp,LEFT);
  44. //这里的left标志是当前深度遍历中是左子树还是右子树。
  45. build(idx+,inR,postL+lnum,postR-,tmp,RIGHT);
  46. }
  47. void dfs(int root,int layer){
  48. if(root==-)
  49. return;
  50. maxlayer=max(layer,maxlayer);
  51. level[layer][levelcnt[layer]]=root;
  52. levelcnt[layer]++;
  53. dfs(node[root].left,layer+);
  54. dfs(node[root].right,layer+);
  55. }
  56. int main()
  57. {
  58. int n;
  59. cin>>n;
  60. for(int i=;i<=n;i++)
  61. cin>>inorder[i];
  62. for(int i=;i<=n;i++)
  63. cin>>postorder[i];
  64. build(,n,,n,-,-);
  65. dfs(,);
  66. bool flag=true;
  67. printf("%d",node[].val);
  68. for(int i=;i<=maxlayer;i++){
  69. if(flag){
  70. for(int j=;j<levelcnt[i];j++)
  71. printf(" %d",node[level[i][j]].val);
  72. }
  73. else{
  74. for(int j=levelcnt[i]-;j>=;j--)
  75. printf(" %d",node[level[i][j]].val);
  76. }
  77. flag=!flag;
  78. }
  79. return ;
  80. }

1.这个是使用函数建树,具体的代码理解在代码里。

PAT 1127 ZigZagging on a Tree[难]的更多相关文章

  1. PAT 1127 ZigZagging on a Tree

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

  2. PAT甲级1127. ZigZagging on a Tree

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

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

  6. PAT 甲级 1127 ZigZagging on a Tree

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

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

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

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

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

随机推荐

  1. SVN目录权限设置

    ---恢复内容开始--- 如图,这里我建的项目库为myRepositories,其下边又有许多文件,现在要分别对每个文件进行svn权限配置. 配置 进入上面生成的文件夹conf下,进行配置.有以下几个 ...

  2. 【java】 java 设计模式(1):工厂方法模式(Factory Method)

    工厂方法模式分为三种: 1.普通工厂模式,就是建立一个工厂类,对实现了同一接口的一些类进行实例的创建.首先看下关系图: 举例如下:(我们举一个发送邮件和短信的例子) 首先,创建二者的共同接口:   p ...

  3. 第四章 Spring.Net 如何管理您的类___对象、对象工厂和应用程序上下文

    在前面一章我们介绍了依赖注入,控制反转的概念,以及自己动手搭建了一下Spring.Net的环境.通过这些操作,我们知道了Spring.Net 的核心是使用依赖注入或控制反转这种思想来管理业务对象,降低 ...

  4. Activity、Window和View三者间的关系有一定的见解

    一.简述如何将Activity展现在手机上 Tips: Activity本身是没办法处理显示什么控件(view)的,是通过PhoneWindow进行显示的 换句话说:activity就是在造Phone ...

  5. C++异常 返回错误码

    一种比异常终止更灵活的方法是,使用函数的返回值来指出问题.例如,ostream类的get(void)成员ASCII码,但到达文件尾时,将返回特殊值EOF.对hmean()来说,这种方法不管用.任何树脂 ...

  6. nginx服务器配置说明

    总结nginx的一些配置选项: nginx全局配置文件 # 定义nginx运行的用户和组//一个默认同时为用户和组 //没有则默认为nobody user www-data; # nginx进程数,建 ...

  7. SQL 根据日期精确计算年龄

    SQL 根据日期精确计算年龄 第一种: 一张人员信息表里有一人生日(Birthday)列,跟据这个列,算出该人员的年龄 datediff(year,birthday,getdate()) 例:birt ...

  8. thinkphp---自动验证的问题

    这段时间做一个项目:使用 thinkphp 做了一个自动验证,但是发现如果新增的时候,是能够进行自动验证的,但是在修改的修改的时候,会发现自动验证会失效. 验证的时候,模型是这样写的: protect ...

  9. angularJS表达式详解!

    angularJS的表达式很像Javascript里的表达式:它可以包含文字,运算符和变量: angularJS 表达式: - 数字:{{100+100}} - 字符串:{{‘hello’+'angu ...

  10. postgresql----LIKE和SIMILAR TO

    LIKE和SIMILAR TO都支持模糊查询,另外SIMILAR TO还支持正则表达式查询.模糊查询中有两个重要的符号:下划线'_'匹配任意单个字符,百分号'%'匹配任意多个字符,可以是0个,如果想匹 ...