【PAT甲级 - C++题解】1127 ZigZagging on a Tree

简介: 【PAT甲级 - C++题解】1127 ZigZagging on a Tree

1127 ZigZagging on a Tree


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.



3ad843f898cb4a0cbffe21e70dd978d3.jpg



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
• 1
• 2
• 3

Sample Output:

1 11 5 8 17 12 20 15


题意

给定中序和后序遍历结果,确定一颗二叉树。

要求输出该二叉树 Z 字形遍历的结果,Z 字形遍历规则如下:

类似于层次遍历,第一层从右往左遍历,第二层从左往右遍历,第三层再从右往左遍历,以此类推。。。


思路


  1. 由于题目给定每个结点的值都不同,所以可以用哈希表存储每个结点在中序数组中的下标,方便后续使用。
  2. 通过下标构建二叉树(其中 k-1-il 表示左子树结点个数):


cb61751803dd470fa38437ca3a8e814a.png



  1. 通过 bfs 进行 Z 字形层次遍历,并输出最终结果。这里有个技巧就是在原有的层次遍历过程中加一个附加条件 step ,只要 step 等于奇数,就将当层的遍历结果进行反转,然后加入到答案中。

代码

#include<bits/stdc++.h>
using namespace std;
const int N = 40;
int n;
int in[N], post[N];
unordered_map<int, int> l, r, pos;
int q[N];
int build(int il, int ir, int pl, int pr)
{
    int root = post[pr];  //获得根结点
    int k = pos[root];    //获得根结点在中序数组中的下标
    if (il < k)    l[root] = build(il, k - 1, pl, pl + k - il - 1);
    if (ir > k)    r[root] = build(k + 1, ir, pl + k - il - 1 + 1, pr - 1);
    return root;
}
void bfs(int root)
{
    int hh = 0, tt = 0;
    q[0] = root;
    int step = 0;
    while (hh <= tt)
    {
        int head = hh, tail = tt;
        while (hh <= tail)
        {
            int t = q[hh++];
            if (l.count(t))   q[++tt] = l[t];
            if (r.count(t))   q[++tt] = r[t];
        }
        if (++step % 2)    reverse(q + head, q + tail + 1); //Z字形遍历
    }
}
int main()
{
    cin >> n;
    //输入中序数组,并记录每个元素在中序数组中的下标位置
    for (int i = 0; i < n; i++)
    {
        cin >> in[i];
        pos[in[i]] = i;
    }
    //输入后序数组
    for (int i = 0; i < n; i++)    cin >> post[i];
    int root = build(0, n - 1, 0, n - 1);
    bfs(root);
    cout << q[0];
    for (int i = 1; i < n; i++)    cout << " " << q[i];
    cout << endl;
    return 0;
}


目录
相关文章
|
C++
【PAT甲级 - C++题解】1040 Longest Symmetric String
【PAT甲级 - C++题解】1040 Longest Symmetric String
160 0
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
210 0
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
199 0
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
196 0
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
121 0
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
139 0
|
人工智能 BI C++
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
273 0
|
存储 定位技术 C++
【PAT甲级 - C++题解】1091 Acute Stroke
【PAT甲级 - C++题解】1091 Acute Stroke
127 0
|
8月前
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
|
4月前
|
人工智能 机器人 编译器
c++模板初阶----函数模板与类模板
class 类模板名private://类内成员声明class Apublic:A(T val):a(val){}private:T a;return 0;运行结果:注意:类模板中的成员函数若是放在类外定义时,需要加模板参数列表。return 0;
113 0