「1119」Pre- and Post-order Traversals

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, or preorder and inorder traversal sequences. However, if only the postorder and preorder traversal sequences are given, the corresponding tree may no longer be unique.

Now given a pair of postorder and preorder traversal sequences, you are supposed to output the corresponding inorder traversal sequence of the tree. If the tree is not unique, simply output any one of them.

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 preorder 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, first printf in a line Yes if the tree is unique, or No if not. Then print in the next line the inorder traversal sequence of the corresponding binary tree. If the solution is not unique, any answer would do. It is guaranteed that at least one solution exists. 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:

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

Sample Output 1:

Yes
2 1 6 4 7 3 5

Sample Input 2:

4
1 2 3 4
2 4 3 1

Sample Output 2:

No
2 1 3 4

Ω

给出一棵二叉树的前序和后续遍历,且每个节点编号唯一,请判断这棵二叉树是否唯一,随后输出这棵二叉树的中序遍历。如果不唯一则输出任意一种情况。

为什么单靠前序和后续遍历结果不能确定唯一二叉树呢?对于树的遍历问题,我曾经说过最重要的莫过于对左右子树区间的划分。前序遍历可以得知P后面的节点可能是P的子节点,后续遍历告诉我们P前面的节点可能是P的子节点,两者的交集即为P的所有子节点。在得到所有子节点的遍历序列后,我们需要划分左右子树,而不确定性的问题就在这,如果这个节点只有一边的子树,那么你就无法确定它是左子树还是右子树:ABC,BCA,但你无法得知AD,DA中的D到底是B还是C。

由于最终的中序遍历可以输出任意情况,于是我们都假设一个节点如果存在子节点那么必有左子树。从而在我们获得子节点区间后,我们可以肯定第一个子节点必然是左子树的根节点,然后在后续遍历结果中找到该节点的位置,两者一夹就是左子树的根节点区间,同时其余部分就是右子树。由此递归下去,在分析树结构的同时,直接将生成中序遍历。

遍历过程中只要有一棵子树的开始索引>末端索引就说明其父节点只有左子树(开始索引=末端索引 表明无子节点),从而可以得出不唯一的结论。


🐎

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

using namespace std;
vector<int> pre, post, in;

bool inOrder(int pre_b, int pre_e, int post_b, int post_e)
{
    if (pre_b == pre_e)
    {
        in.push_back(pre[pre_b]);
        return true;
    }
    else if (pre_b > pre_e) return false;
    int bias = (find(post.begin(), post.end(), pre[pre_b + 1]) - post.begin()) - post_b;
    bool left = inOrder(pre_b + 1, pre_b + 1 + bias, post_b, post_b + bias);
    in.push_back(pre[pre_b]);
    bool right = inOrder(pre_b + bias + 2, pre_e, post_b + bias + 1, post_e - 1);
    return left && right;
}


int main()
{
    int n;
    cin >> n;
    pre.resize(n), post.resize(n);
    for (auto &k: pre)
        cin >> k;
    for (auto &k: post)
        cin >> k;
    if (inOrder(0, n - 1, 0, n - 1))
        cout << "Yes\n";
    else cout << "No\n";
    printf("%d", in[0]);
    for (int i = 1; i < in.size(); ++i)
        printf(" %d", in[i]);
    printf("\n");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值