zoukankan      html  css  js  c++  java
  • 真二叉树重构(Proper Rebuild)

    清华OJ——数据结构与算法实验(中国石油大学)

    真二叉树重构(Proper Rebuild)


    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

    提示

    观察左、右孩子在先序、后序遍历序列中的位置

    重温视频05e5-3

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #define N 4000500
     5 using namespace std;
     6 
     7 struct ss
     8 {
     9     int lson,rson;
    10 };
    11 ss tree[N];
    12 int a[N],b[N];
    13 long long pa[N]={0},pb[N]={0};
    14 
    15 
    16 void dfs(int l1,int r1,int l2,int r2)
    17 {
    18     if(l1>r1)return;
    19     if(l1==r1)return;
    20 
    21     int root=a[l1];
    22     l1++;
    23     r2--;
    24 
    25     int mid1,mid2;
    26     for(mid1=l1,mid2=l2;;mid1++,mid2++)
    27     {
    28         if(a[l1]==b[mid2])break;
    29     }
    30 
    31     tree[root].lson=a[l1];
    32     tree[root].rson=a[mid1+1];
    33 
    34     dfs(l1,mid1,l2,mid2);
    35     dfs(mid1+1,r1,mid2+1,r2);
    36     return ;
    37 }
    38 
    39 void print(int x)
    40 {
    41     if(tree[x].lson)print(tree[x].lson);
    42     printf("%d ",x);
    43     if(tree[x].rson)print(tree[x].rson);
    44 }
    45 
    46 int main()
    47 {
    48     int n;
    49     scanf("%d",&n);
    50     for(int i=1;i<=n;i++)scanf("%d",&a[i]),pa[i]=pa[i-1]+a[i];
    51     for(int i=1;i<=n;i++)scanf("%d",&b[i]),pb[i]=pb[i-1]+b[i];
    52     for(int i=1;i<=n;i++)tree[i].lson=tree[i].rson=0;
    53 
    54     dfs(1,n,1,n);
    55 
    56     int root=a[1];
    57     print(root);
    58     return 0;
    59 }
  • 相关阅读:
    安装好Ruby,使用Windows Powershell不能使用
    关于css注释在sublime中的问题
    腾讯云服务器Centos使用心得
    火狐浏览器打开后出现两个主页,其中一个为hao123
    找兴趣点切入
    canvas绘制饼状图
    关于(function(){})(); 的一些理解
    sublime标签页不能显示的解决方法
    ubuntu下jdk安装与环境配置
    Array和ArrayList的Clone为什么一个不用类型转换,一个要类型转换
  • 原文地址:https://www.cnblogs.com/sylvia1111/p/15613030.html
Copyright © 2011-2022 走看看