In computer science, a perfect binary tree is a binary tree in which all non-leaf nodes have two children and all leaves have the same depth. In a complete binary tree, every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. A pseudo-complete binary tree is a binary tree that becomes a perfect tree when the bottom level is removed.
Given the inorder and preorder traversal sequences of a binary tree, you are supposed to tell the type of this tree, and output its postorder traversal sequence.
Input Specification:
Each input file contains one test case. The first line gives a positive integer N (≤2000) which is the number of nodes in the tree. Then the following two lines contain the inorder and preorder traversal sequences of the binary tree, respectively. It is guaranteed that all the keys are distinct, and in the range of int. All the numbers in a line are separated by a space.
Output Specification:
For each test case, first print in a line the type of the given tree: 1 for perfect binary tree, 2 for complete binary tree (but not of type 1), 3 for pseudo-complete binary tree (but not of types 1 or 2); and 0 for a binary tree of other types. Then in the second line, output the postorder traversal sequence of the tree. All the numbers in a line are separated by 1 space, and there must be no extra space at the beginning or the end of the line.
Sample Input 1:
7
4 2 5 1 6 3 7
1 2 4 5 3 6 7
Sample Output 1:
1
4 5 2 6 7 3 1
Sample Input 2:
10
8 4 9 2 10 5 1 6 3 7
1 2 4 8 9 5 10 3 6 7
Sample Output 2:
2
8 9 4 10 5 2 6 7 3 1
Sample Input 3:
10
8 4 2 5 11 1 6 3 14 7
1 2 4 8 5 11 3 6 7 14
Sample Output 3:
3
8 4 11 5 2 6 14 7 3 1
Sample Input 4:
7
4 2 10 5 1 3 7
1 2 4 5 10 3 7
Sample Output 4:
0
4 10 5 2 7 3 1
#include<iostream>
#include<unordered_map>
#include<cmath>
using namespace std;
const int N = 2010;
int n;
unordered_map<int,int> l,r,pos,depth;
int pre[N],in[N],max_depth,nodes;
void cnt(int u){
if(depth[u] == max_depth) nodes++;
if(l.count(u)) cnt(l[u]);
if(r.count(u)) cnt(r[u]);
}
void max_d(int u,int d){
max_depth = max(d,max_depth);
if(l.count(u)) max_d(l[u],d+1);
if(r.count(u)) max_d(r[u],d+1);
}
int build(int pl,int pr,int il,int ir,int d){
int root = pre[pl];
depth[root] = d;
int k = pos[root];
if(k>il) l[root] = build(pl+1,pl+k-il,il,k-1,d+1);
if(k<ir) r[root] = build(pl+k-il+1,pr,k+1,ir,d+1);
return root;
}
bool is_perfect(){
return n==pow(2,max_depth)-1;
}
bool is_cbt(int u,int k){
if(k>=n) return false;
bool lans = 1,rans = 1;
if(l.count(u)) lans = is_cbt(l[u],2*k+1);
if(r.count(u)) rans = is_cbt(r[u],2*k+2);
return lans&&rans;
}
bool is_pse(){
return (n-nodes) == pow(2,max_depth-1)-1;
}
int flag = 1;
void dfs(int u){
if(l.count(u)) dfs(l[u]);
if(r.count(u)) dfs(r[u]);
if(flag) flag = 0;
else cout<<" ";
cout<<u;
}
int main(){
scanf("%d",&n);
for(int i = 0;i<n;i++){
cin>>in[i];
pos[in[i]] = i;
}
for(int i = 0;i<n;i++)cin>>pre[i];
int root = build(0,n-1,0,n-1,1);
max_d(root,1);
cnt(root);
if(is_perfect())puts("1");
else if(is_cbt(root,0))puts("2");
else if(is_pse())puts("3");
else puts("0");
dfs(root);
}