1119 Pre- and Post-order Traversals (30分)

110 篇文章 0 订阅

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

这里直接转载一下别人的图,方便以后翻阅理解
已知树的先序遍历和后序遍历,求树的中序遍历,如果答案是不唯一的输出No,后输出任意一中序遍历,如果答案唯一,输出Yes,后输出中序遍历
先查看中序遍历是否唯一,先序遍历顺序为:根左右,后序遍历顺序为:左右根,那么如果一个节点只包含左子节点或者只包含右子节点,但是根据上面的顺序可以发现,他们的先序遍历和后续遍历是没有发生变化的,例如:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
此图中节点1,节点3,只含有左子节点或者右子节点,但是他们的先序遍历和后续遍历都是相同的:

先序遍历:1 2 5 3 4

后序遍历:5 4 3 2 1

所以只要父亲节点只包含一个子节点,那么这个答案就是不唯一的

后序遍历和先序遍历得到中序遍历(栗一为例)
在这里插入图片描述
先序遍历: 1 2 3 4 6 7 5 后序遍历: 2 6 7 4 5 3 1
在先序遍历中先取出开始的节点1,这个节点定为当前根节点,1后面一个节点2一定是节点1的一个子节点,然后在后序遍历中寻找节点2,于是便可知节点2(包括2)之前的位于节点1的左子树中,2(不包括2)后面的节点直至节点1,位于节点1的右子树
自我理解,因为对于后序遍历而言是左右根的,所以子树的根是在其孩子结点之后的,所以2之前的结点都是1的左子树的,因为2是1的左子结点。2后面的都是右子树的
节点 3 4 6 7 5位于节点1的右子树中,根据先序遍历可知3为这个子树的根节点,节点4为节点3的左或右子节点中的一个,后序遍历中找到节点4,可以知道节点6 7 4位于节点3的左子树中,节点5位于节点3的右子树中
自我理解,因为3是右子树的根结点,因为先序遍历是左右根的,所以3后面那个结点就是其左子树的根结点,也就是4。在后序遍历找到4,后序遍历是左右根的,所以4前面的都是它的子结点,4后面的都是3的右子结点,不包括3.
最后一次,前序遍历中,4后面就是6,所以6是4的左子树的根结点,后序遍历6前面的就是4的左子树,包括6,6后面的就是右子树。
依次类推,可以遍历完这棵树,于是便可得到中序遍历

如果上述中找到只包含一个子节点的父节点,没有发现另外一个节点,即当前遍历区间中只包含当前节点的一个子节点,说明这个树是不唯一的,我们默认情况下将其安排在左子节点中
如何判断当前结点的子节点只有一个,看一下上面我们是如何划分左右子树的,如果左子树只包含当前划分的结点,右子树为0,那么证明它只有一个结点,默认将其放入左子树。

#include <iostream>
#include <initializer_list>
#include <algorithm>
#include <string>
#include <string.h>
#include <thread>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#define INF 0x3f3f3f3f
 
using namespace std;
const int maxn = 35;
int n,pre[maxn],post[maxn],in[maxn],_index;
bool flag = false;
void Init()
{
    scanf("%d",&n);
    for(int i =1 ;i <= n;i ++)
        scanf("%d",pre+i);
    for(int i = 1;i <= n;i ++)
        scanf("%d",post+i);
}
void Build_tree(int l1,int r1,int l2,int r2)
{
    if(l1 > r1) return  ;
    if(l1 == r1)
    {
        in[++_index] = pre[l1]; return ;        //中序遍历中的子节点
    }
    int cnt = 0;
    while(post[l2+cnt] != pre[l1+1]) cnt++;
    cnt++;                                     //包括pre[l1+1]本身,所以要+1
    if(cnt == 1 && l1+cnt == r1)               //只包含当前子节点,并且区间中只有此一个节点
        flag = true;
 
    Build_tree(l1+1,l1+cnt,l2,l2+cnt-1);         //遍历左子区间
    in[++_index] = pre[l1];                     //中序遍历中的根节点
    Build_tree(l1+cnt+1,r1,l2+cnt,r2-1);        //遍历右子区间
}
int main()
{
    Init();
    Build_tree(1,n,1,n);
    if(flag)
        printf("No\n");
    else
        printf("Yes\n");
    for(int i = 1;i <= n;i ++)
        printf("%d%c",in[i],i==n?'\n':' ');
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值