Binary Tree Restoring

题目链接:题目

Binary Tree Restoring


Time Limit: 1 Second      Memory Limit: 65536 KB      Special Judge


 

Given two depth-first-search (DFS) sequences of a binary tree, can you find a binary tree which satisfies both of the DFS sequences?

Recall that a binary tree is a tree in which each vertex has at most two children, and the depth-first search is a tree traversing method which starts at the root and explores as far as possible along each branch before backtracking.

Input

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains an integer n (1 ≤ n ≤ 105), indicating the number of vertices in the binary tree.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n, ∀ 1 ≤ i < j ≤ n, ai ≠ aj), indicating the first DFS sequence of the binary tree.

The third line of each test case contains n integers b1, b2, ..., bn (1 ≤ bi ≤ n, ∀ 1 ≤ i < j ≤ n, bi ≠ bj), indicating the second DFS sequence of the binary tree.

It is guaranteed that the sum of n over all test cases does not exceed 106, and there always exists at least one possible binary tree.

We kindly remind you that this problem contains large I/O file, so it's recommended to use a faster I/O method. For example, you can use scanf/printf instead of cin/cout in C++.

Output

For each test case, output one line which contains n integers seperated by one space. The i-th integer indicates the father of the i-th vertex in the binary tree which satisfies both of the DFS sequence. If the i-th vertex is the root of the binary tree, output 0 as its father. If there are multiple valid answers, you can output any of them.

Please, DO NOT print extra spaces at the end of each line, or your program may get a "wrong answer" verdict as this problem is special judged.

Sample Input

2
6
3 4 2 5 1 6
3 4 5 2 1 6
3
1 2 3
1 2 3

Sample Output

3 4 0 3 4 1
0 1 2

 

题意:

给定二组序列,分别为同一个二叉树的 DFS 序列,输出二叉树各节点的父亲。

思路:

对 DFS 结果序列进行模拟,可以类似的参考树的前、中、后序互相转换的递归思路。

 

只有两种情况:

(1)当前节点在两个串中后面的节点假如不同则能确认两个子树

(2)如果相同则把下个点作当前点的一个儿子。如果子树中还有未连根的点则接到当前点下。

 

用样例或者更复杂的串可以模拟一下,这样比较简单:如例:3 4 2 5 7 1 6和3 1 6 4 5 7 2 

 

 

用深搜的方式不断分割左子树和右子树!!!

#include<bits/stdc++.h>
using namespace std;
const int N = 100000 + 10;
int a[N], b[N];         /// 序列 A, B
int posa[N], posb[N];   /// 节点值 i 在序列 A(B) 中的位置  哈希表
int par[N];              /// 记录节点值 i 的父节点值
int pnt;                /// pnt-1 表示当前已知道了多少个节点的父节点 -> 即 pnt 为之后需处理第 pnt 个点的父节点
void solve(int pos,int l,int r,int fa){ ///当前需处理 A[pos] 的父节点,对应 B 序列的区间为 [l,r]
    if(l>r) return;
    if(a[pos]==b[l]){///如果当前要确定的节点和第二个串第一个字母是一样的
        par[a[pos]]=fa;
        pnt++;
        solve(pos+1,l+1,r,a[pos]);
        if(pnt-pos<=r-l){  ///如果子树中还有未连根的点则接到当前点下。
            int tmp=pnt;
            par[a[tmp]]=fa;
            pnt++;
            solve(tmp+1,l+tmp-pos+1,r,a[tmp]);
        }
    }
    else{
        par[a[pos]]=fa;
        pnt++;
        int len1=posa[b[l]]-pos-1;
        solve(pos+1,posb[a[pos]]+1,posb[a[pos]]+len1,a[pos]);  ///其中一个子树
        par[b[l]]=fa;
        pnt++;
        solve(posa[b[l]]+1,l+1,posb[a[pos]]-1,b[l]);          ///另外一个子树
    }
}
int main()
{
    int T,n;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;++i)
            scanf("%d",&a[i]),posa[a[i]]=i;
 
        for(int i=1;i<=n;++i)
            scanf("%d",&b[i]),posb[b[i]]=i;
 
        pnt=1;
        solve(1,1,n,0);
        for(int i=1;i<=n;++i){
            if(i>1) printf(" ");
            printf("%d",par[i]);
        }
        printf("\n");
    }
}
posted @ 2020-09-13 13:34  Shmilky  阅读(70)  评论(0编辑  收藏  举报