#include #include #include #include #include #include #include #include #include using namespace std; #define INF 100000000 #define maxn 100010 int pre[maxn]; int post[maxn]; int in[maxn]; int m; struct node{ int num; node* lchild; node* rchild; }; node* newnode(node *&root,int num) { root=new node; root->num=num; root->lchild=NULL; root->rchild=NULL; return root; } node * create(int posl,int posr,int inl,int inr) { node *root; if(posl>posr) return NULL; root=newnode(root,post[posr]); // root->num=post[posr]; int k; for(k=inl;k<=inr;k++) if(in[k]==post[posr]) break; int numleft=k-inl; root->lchild=create(posl,posl+numleft-1,inl,k-1); root->rchild=create(posl+numleft,posr-1,k+1,inr); return root; } void BFS(node *root ) { queue q;//注意定义了一个指针类型的 q.push(root); int i=0; while(!q.empty()) { i++; node *temp; temp=q.front(); cout< num; if(i q.pop(); if(temp->lchild!=NULL) q.push(temp->lchild); if(temp->rchild!=NULL) q.push(temp->rchild); } } int main() { cin>>m; for(int i=0;i cin>>post[i]; for(int i=0;i cin>>in[i]; node *root; root=create(0,m-1,0,m-1); BFS(root); return 0; }