重构真二叉树

原题是在Tsinghua OJ:真二叉树重构(Proper Rebuild)

https://dsa.cs.tsinghua.edu.cn/oj/course.shtml?courseid=58

Description


In general, given the preorder traversal sequence and postorder traversal sequence of a binary tree, we cannot determine the binary tree.


在这里插入图片描述

Figure 1

In Figure 1 for example, although they are two different binary tree, their preorder traversal sequence and postorder traversal sequence are both of the same.

But for one proper binary tree, in which each internal node has two sons, we can uniquely determine it through its given preorder traversal sequence and postorder traversal sequence.

Label n nodes in one binary tree using the integers in [1, n], we would like to output the inorder traversal sequence of a binary tree through its preorder and postorder traversal sequence.

Input

The 1st line is an integer n, i.e., the number of nodes in one given binary tree,
The 2nd and 3rd lines are the given preorder and postorder traversal sequence respectively.

Output

The inorder traversal sequence of the given binary tree in one line.

Example

Input

5
1 2 4 5 3
4 5 2 3 1

Output

4 2 5 1 3

Restrictions

For 95% of the estimation, 1 <= n <= 1,000,00

For 100% of the estimation, 1 <= n <= 4,000,000

The input sequence is a permutation of {1,2…n}, corresponding to a legal binary tree.

Time: 2 sec

Memory: 256 MB

Hints

Figure 2

In Figure 2, observe the positions of the left and right children in preorder and postorder traversal sequence.

描述

一般来说,给定二叉树的先序遍历序列和后序遍历序列,并不能确定唯一确定该二叉树。

在这里插入图片描述

(图一)

比如图一中的两棵二叉树,虽然它们是不同二叉树,但是它们的先序、后序遍历序列都是相同的。

但是对于“真二叉树”(每个内部节点都有两个孩子的二叉树),给定它的先序、后序遍历序列足以完全确定它的结构。

将二叉树的n个节点用[1, n]内的整数进行编号,输入一棵真二叉树的先序、后序遍历序列,请输出它的中序遍历序列。

描述

第一行为一个整数n,即二叉树中节点的个数。

第二、三行为已知的先序、后序遍历序列。

描述

仅一行,给定真二叉树的中序遍历序列。

描述

见英文题面

描述

对于95%的测例:1 ≤ n ≤ 1,000,000

对于100%的测例:1 ≤ n ≤ 4,000,000

输入的序列是{1,2…n}的排列,且对应于一棵合法的真二叉树

时间:2 sec

空间:256 MB

提示
在这里插入图片描述
观察左、右孩子在先序、后序遍历序列中的位置


本来昨天在mooc上看到了邓俊辉老师的数据结构课程,本来只前面讲的(前序|后序)和中序的二叉树重构在课上也听到一些,理解起来不是太难,但是这个,前后序重构真二叉树着实有点傻。

重构真二叉树

真二叉树(proper binary tree): 每个节点的度都为偶数(2|0),
所以不存在左空右不空的情形,从而无法判断节点位置。根据前后遍历序列,可以重构二叉树,下面借邓俊辉老师的课件插图说明(感兴趣的可点击这里看一看):
在这里插入图片描述
  如上图,前序遍历的第一个节点必然是根节点,跟随其后的也必然是根节点的左孩子,可以在后序遍历序列中找到它的位置;同理后序遍历序列中最后一个元素必然是根节点,跟随其后的也是根节点的右孩子,可在前序遍历序列中找到位置。

  这样,我们就分别划分出了前后遍历序列中左右字数的分界。再通过递归的方法就可以重构二叉树。

下面是错误代码,还没有改过来。

喜大普奔啊,
晚上又看了一下网上各位大佬的的方案,总算是解决了。下面是个95分的答案,case20 (runtime error)总是过不去,等有思路了再改吧,

如有不当,请多指正.

#include <iostream>

using namespace std;
/**************
7
1 2 4 5 3 6 7
4 5 2 6 7 3 1
**************/
struct BinNode
{
    int data;//数据
    BinNode *lc;//左孩子
    BinNode *rc;//右孩子
    BinNode() : lc ( NULL ), rc ( NULL ) {}
    BinNode ( int _data ) : data ( _data ), lc ( NULL ), rc ( NULL ) {}
};

BinNode* rebuild ( int length, int *pre, int *post )
{
	BinNode* root = new BinNode;//创建一个新节点
    root->data = pre[0];//以新节点为根,数据域为pre序列的第一个数
    if (length < 2)//递归结束
        return 0;
    int mid_left;//记录左子树根节点在postOrder中的位置
    for ( mid_left = 0; mid_left < length; mid_left++ )
    {
        if ( pre[1] == post[mid_left] )
            break;
    }
    //左子树节点个数为mid_left+1
    //右子树节点个数为length-1-(mid_left+1)
    root->lc = rebuild ( mid_left + 1, pre + 1, post );
    root->rc = rebuild ( length - mid_left - 2, pre + mid_left + 2, post + mid_left + 1 );
    return root;
}

//中序遍历
void inOrder(BinNode *root)
{
    if (root->lc)
        inOrder ( root->lc );
    cout<<root->data;
    if (root->rc)
        inOrder ( root->rc );
}

int main()
{
    int length;//节点个数
    cin >> length;
    int pre[length], post[length];
    for ( int i = 0; i < length; i++ )
    {
        cin >> pre[i];
    }
    for ( int i = 0; i < length; i++ )
    {
        cin >> post[i];
    }

    BinNode* root;
    root = rebuild ( length, pre, post );

    inOrder ( root );

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值