1119 Pre- and Post-order Traversals (30 分)前序、后序序列转为中序

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
思路
  1、这个题目是我看了别人的答案才做出的。
   2、我们知道已知二叉树的前序序列和后序序列,无法恢复这个二叉树。为什么?在某一个根节点只有左子树或者右子
树的时候,我们根据前序和后序序列无法确定它究竟是左子树还是右子树。也就是说,如果一个二叉树的所有节点都是度为
0或者度为2(不一定是满二叉树,比如哈夫曼树也满足条件)的时候,根据前序和后序序列可以唯一恢复重建这棵树。对于
不确定的情况应该怎么办呢?该题要求我们输出任意一种就可以了,而不确定是指不知道某个子树是根的左子树还是右子树
,我的代码假定为右子树,当然你也可以选择左子树。
  3、详情请看代码。
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<string.h>
#include<cstdio>
#include<cmath>
using namespace std;
int preOrder[35];
int postOrder[35];

vector<int>inOrder;

struct Node
{
    int data;
    Node *lchild=nullptr;
    Node *rchild=nullptr;
};
bool flag=true;
//当一个结点只有左子树或右子树的时候会有不同的结果
void createTree(Node *&root,int preOrder[],int pl,int pr,
                int postOrder[],int postL,int postR)
{
    if(root==nullptr)
    {
        root=new Node;
        root->data=preOrder[pl];
    }
    if(postR==postL)//z该子树只有一个元素不需要再划分了
        return;
    int index=0;
    //在后序遍历序列中,根节点之前的元素(本程序认为是右子树,也可能是左子树,无法确定)是右子树的根,
    //查找右子树的根在前序遍历序列中的位置,这样可以进行根,左右子树序列的划分。
    while(preOrder[index]!=postOrder[postR-1])
        index++;
    //index指向的是前序序列中右子树的根(如果有歧义,默认为右子树)
    int len=index-pl-1;
   // int rlen=pr-index+1;
    if(len>0)
        createTree(root->lchild,preOrder,pl+1,pl+len,postOrder,postL,postL+len-1);
    else//如果右子树的根和左子树之间无元素,则不知道这个子树是左子树还是右子树,划分为右子树
        flag=false;
    //右子树至少有一个元素,所以不需要判断rlen的值
    createTree(root->rchild,preOrder,index,pr,postOrder,postL+len,postR-1);
}

void inoder(Node *root)
{
    if(root)
    {
        inoder(root->lchild);
        inOrder.push_back(root->data);
        inoder(root->rchild);
    }
}

int main()
{
    int n;
    cin>>n;
    for(int i=0; i<n; i++)
        cin>>preOrder[i];
    for(int i=0; i<n; i++)
        cin>>postOrder[i];


    Node *root=nullptr;

    createTree(root,preOrder,0,n-1,postOrder,0,n-1);
    inoder(root);
    if(flag)
    {
        cout<<"Yes"<<endl;

    }
    else
        cout<<"No"<<endl;
    cout<<inOrder[0];
    for(int i=1; i<inOrder.size(); i++)
        cout<<" "<<inOrder[i];
    cout<<endl;
    return 0;
}


原文地址:https://www.cnblogs.com/zhanghaijie/p/10316952.html