【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
66 0
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
82 0
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
77 0
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
76 0
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
79 0
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
80 0
|
人工智能 BI C++
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
136 0
|
存储 定位技术 C++
【PAT甲级 - C++题解】1091 Acute Stroke
【PAT甲级 - C++题解】1091 Acute Stroke
58 0
|
1天前
|
存储 编译器 C语言
【c++丨STL】string类的使用
本文介绍了C++中`string`类的基本概念及其主要接口。`string`类在C++标准库中扮演着重要角色,它提供了比C语言中字符串处理函数更丰富、安全和便捷的功能。文章详细讲解了`string`类的构造函数、赋值运算符、容量管理接口、元素访问及遍历方法、字符串修改操作、字符串运算接口、常量成员和非成员函数等内容。通过实例演示了如何使用这些接口进行字符串的创建、修改、查找和比较等操作,帮助读者更好地理解和掌握`string`类的应用。
13 2
|
7天前
|
存储 编译器 C++
【c++】类和对象(下)(取地址运算符重载、深究构造函数、类型转换、static修饰成员、友元、内部类、匿名对象)
本文介绍了C++中类和对象的高级特性,包括取地址运算符重载、构造函数的初始化列表、类型转换、static修饰成员、友元、内部类及匿名对象等内容。文章详细解释了每个概念的使用方法和注意事项,帮助读者深入了解C++面向对象编程的核心机制。
30 5