tree reconstruction

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> pre,in,post;
int n,pos;
void rec(int l,int r) {
if (l>=r) return ;
int root=pre[pos++]; //Determine the node
int m=distance(in.begin(), find(in.begin(),in.end(),root)); //The position of the early arrival node at in determines the position of the child node

rec(l,m); //Left node recursion

rec(m+1,r); // right node recursion
post.push_back(root); //Press the post stack
}
void selove() {
pos=0;
rec(0,pre.size()); //The result of pre traversal determines the node order
for (int i=0;i<n;i++) {
if (i) cout<<' ';
cout<<post[i];
}
cout << endl;
}
int main() {
int k;
cin>>n;
for (int i=0;i<n;i++) {
cin>>k;
pre.push_back(k);
}
for (int i=0;i<n;i++) {
cin>>k;
in.push_back(k);
}
villages ();
return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325405739&siteId=291194637