硬寫的
// 我也不知道多長時間了,估計有40min
#include <bits/stdc++.h>
using namespace std;
const int N = 50;
int hou[N], zhong[N];
class Node
{
public:
int val;
int wei;
Node* father;
Node* left;
Node* right;
Node(int v = -114): val(v), left(NULL), right(NULL), father(NULL){};
};
Node* head;
int main()
{
int n;
cin >> n;
for(int i = 1; i <= n; ++ i)
cin >> hou[i];
for(int i = 1; i <= n; ++ i)
cin >> zhong[i];
for(int i = n; i >= 1; -- i)
{
int nown = hou[i];
int index = 0;
for(int j = 1; j <= n; ++ j)
{
if(zhong[j] == nown)
{
index = j;
break;
}
}
// head為空的特判
if(head == NULL)
{
head = new Node;
head->val = nown;
head->wei = index;
}
else
{
auto tmp = head;
while(tmp)
{
if(index < tmp->wei)
{
if(tmp->right == NULL)
{
tmp->right = new Node;
tmp = tmp->right;
tmp->val = nown;
tmp->wei = index;
break;
}
tmp = tmp->right;
}
else
{
if(tmp->left == NULL)
{
tmp->left = new Node;
tmp = tmp->left;
tmp->val = nown;
tmp->wei = index;
break;
}
tmp = tmp->left;
}
}
}
}
queue<Node*> ans;
ans.push(head);
int cnt = 0;
while(ans.size())
{
++ cnt;
auto tmp = ans.front();
ans.pop();
cout << tmp->val << (cnt == n ? "" : " ");
if(tmp->right != NULL) ans.push(tmp->right);
if(tmp->left != NULL) ans.push(tmp->left);
}
return 0;
}