PAT(A) 1119. Pre- and Post-order Traversals (30)

原题目:

原题链接:https://www.patest.cn/contests/pat-a-practise/1119

1119. Pre- and Post-order Traversals (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, 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

题目大意

根据前序遍历和后序遍历,判断其对应的二叉树是否唯一,若唯一,输出Yes及对应中序遍历,若不唯一,输出No及任意对应的中序遍历。

解题报告

本题是树的题,要求对二叉树掌握比较多。
一般来说,根据遍历建树,大多为中序遍历加个前序或后序。只有前序遍历和后序遍历的话,因为有时无法确定子树是左子树还是右子树。比如前序1 2 3 4和后序2 4 3 1,他的中序可以是2 1 4 3也可以是2 1 3 4,因为无法得知4是3的左子树还是右子树。
可以明确的是:
对于前序遍历,其序列是“ 根 一堆左子树点 一堆右子树点”。
对于后序遍历,其序列是“ 一堆左子树点 一堆右子树点 根”。
其中的前序的左子树点和后序的左子树点在集合上是一致的,个数一样,内容一样(顺序不一定),右子树点同理。
又:
对于前序遍历,如果存在左子树,那么左子树的值必定是前序中第2个数(以1开始)。
对于后序遍历,如果存在右子树,那么右子树的值必定是后序中倒数第2个数(以1开始)。
由是,解题方案有两个:
1. 对每个序列,抛开根节点,看能否找到一个分界线,使得前序后序在分界线两端的元素内容一致(不含顺序),若无法找到(及唯一的一个是包含所以节点,即仅一个子树),那说明构成二叉树不唯一。
2. 对于后序遍历,因根节点前一个节点是其子树,假若这个子树节点在前序遍历中处于第一个位置(抛开根),说明这是唯一的一个子树节点。因为,根据前序后序定义,如果存在两个,那前序是“ 根 一堆左子树点 一堆右子树点”,现在只有一个,说明只有一个子树。
这里采用方法1,方法2可以参照:https://www.liuchuo.net/archives/2484

代码

/*
* Problem: 1119. Pre- and Post-order Traversals (30)
* Author: HQ
* Time: 2018-03-11
* State: Done
* Memo:
*/
#include "iostream"
#include "vector"
#include "set"
using namespace std;

struct Node {
    int data;
    struct Node *left,*right;
};

set<int> pre_left, post_left;
vector<int> pre, post;
bool flag = true;
int N;

void makeTree(struct Node *root, int s1, int s2, int l) {
    root->data = pre[s1];
    if (l == 1) {
        root->left = root->right = NULL;
        return;
    }
    pre_left.clear();
    post_left.clear();
    for (int i = 0; i < l - 1; i++) {
        pre_left.insert(pre[s1 + 1 + i]);
        post_left.insert(post[s2 + i]);
        if (pre_left == post_left)
            break;
    }
    int size = pre_left.size();
    if (size == l - 1) {
        flag = false;
    }
    if (size > 0) {
        root->left = new struct Node;
        makeTree(root->left, s1 + 1, s2, size);
    }
    if (l - 1 - size > 0) {
        root->right = new struct Node;
        makeTree(root->right, s1 + 1 + size, s2 + size, l - 1 - size);
    }
    else
        root->right = NULL;

}

void inOrder(struct Node *root1) {
    static bool first = true;
    if (root1->left != NULL)
        inOrder(root1->left);
    if (first) {
        printf("%d", root1->data);
        first = false;
    }else
        printf(" %d", root1->data);
    if (root1->right != NULL)
        inOrder(root1->right);
}

int main() {
    cin >> N;
    pre.resize(N);
    post.resize(N);
    for (int i = 0; i < N; i++)
        cin >> pre[i];
    for (int i = 0; i < N; i++)
        cin >> post[i];
    struct Node *root = new struct Node;
    makeTree(root,0,0,N);
    printf("%s\n", flag ? "Yes" : "No");
    inOrder(root);
    system("pause");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值