「1020」Tree Traversals

➳ENTRY

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

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 postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding binary tree. 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:

7
2 3 1 5 7 6 4
1 2 3 4 5 6 7

Sample Output:

4 1 6 3 5 7 2

Ω

题意简单明了,就是通过二叉树的后序遍历序列和中序遍历序列求出层次遍历序列。

我们只需抓住后序和中序的重要特征即可,后序的中间节点是在最后的,中序的中间节点则是在左右子树的中间。那么我们可以采取如下算法构建二叉树:

  1. 先取出后序的最后一个数字即可知当前节点的编号

  2. 在中序序列中找到该编号所处的位置,那么中序序列就被该节点划分成了左右两棵子树的中序序列,同时我们也得到了两棵子树的节点个数

  3. 根据得到的左右子树节点个数也可以将后序序列划分成左右两棵子树的后序序列

  4. 如此一来我们就分别得到了左右子树的后序和中序遍历序列,从而划分成了两个子问题再重复上述过程进行解决

很显然,划分为子问题是递归的思想,因此只需码一个递归构建二叉树的函数即可,传入当前子树的后序和中序序列在原序列中的位置即可。


C☺DE

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;
map<int, pair<int, int>> tree;
vector<int> post, in;

void build(int in_start, int in_end, int post_start, int post_end)
{
    if (in_start >= in_end)
        return;
    int crt = post[post_end], in_pos = find(in.begin() + in_start, in.begin() + in_end, crt) - in.begin() - in_start;
    tree[crt].first = (in_pos == 0) ? 0 : post[post_start + in_pos - 1];
    build(in_start, in_start + in_pos - 1, post_start, post_start + in_pos - 1);
    tree[crt].second = (in_pos == in_end - in_start) ? 0 : post[post_end - 1];
    build(in_start + in_pos + 1, in_end, post_start + in_pos, post_end - 1);
}

int main()
{
    int n;
    cin >> n;
    post.resize(n);
    in.resize(n);
    for (int i = 0; i < n; ++i)
        cin >> post[i];
    for (int i = 0; i < n; ++i)
        cin >> in[i];
    build(0, n - 1, 0, n - 1);
    vector<int> level, tmp;
    cout << post[n - 1];
    if (tree[post[n - 1]].first != 0)
        level.push_back(tree[post[n - 1]].first);
    if (tree[post[n - 1]].second != 0)
        level.push_back(tree[post[n - 1]].second);
    while (!level.empty())
    {
        for (auto &k: level)
        {
            cout << " " << k;
            if (tree[k].first != 0)
                tmp.push_back(tree[k].first);
            if (tree[k].second != 0)
                tmp.push_back(tree[k].second);
        }
        level = tmp;
        tmp.clear();
    }
}

Tips

  1. map<int,pair<int,int>>来表示二叉树,通过节点编号可以直接得知左右节点,若节点编号为0则代表该节点为空

  2. 注意左右子树为空的情况,也是递归结束的时候

  3. 在本题中其实可以无需构建出完整的二叉树,因为只需输出层次遍历序列,所以我们可以给每个编号都给予一个层次遍历的索引,最后按索引输出编号即可,详见柳婼 の blog

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值