![在这里插入图片描述](https://img-blog.csdnimg.cn/20200814160327739.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Jid3FzYg==,size_16,color_FFFFFF,t_70#pic_center)
#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<set>
#include<queue>
#include<unordered_set>
using namespace std;
vector<int>pre, in;
struct node
{
int data;
node* left, * right;
};
node* buildtree(int prel, int prer, int inl, int inr)
{
if (prel > prer)
return NULL;
node* root = new node;
root->left = NULL;
root->right = NULL;
root->data = pre[prel];
int i;
for (i = inl; i <=inr; i++)
{
if (in[i] == pre[prel])
{
break;
}
}
int leftnum = i - inl;
root->left=buildtree(prel + 1, prel + leftnum, inl, i - 1);
root->right = buildtree(prel + leftnum + 1, prer, i + 1, inr);
return root;
}
node* findleft(node* root)
{
if (root->left == NULL && root->right == NULL)
return root;
if (root->left!=NULL)
{
root = root->left;
}
else if(root->right!=NULL)
{
root = root->right;
}
findleft(root);
}
int main()
{
int n,temp; cin >> n;
for (int i = 0; i < n; i++)
{
cin >> temp;
pre.push_back(temp);
}
for (int i = 0; i < n; i++)
{
cin >> temp;
in.push_back(temp);
}
node* root = new node;
root = buildtree(0, n - 1, 0, n - 1);
node* left = new node;
left = findleft(root);
cout << left->data << endl;
}