给出一个棵二叉树的后序遍历和中序遍历,求二叉树的层序遍历
#include<bits/stdc++.h> using namespace std; const int N=50; int in[N]; int post[N]; typedef struct node; typedef node *tree; struct node { int data; tree L; tree R; }; void print(tree &bt,int l1,int r1,int l2,int r2) { if(l1>r1||l2>r2) return; int mid=l2; while(in[mid]!=post[r1]) mid++; bt=new node; bt->data=post[r1]; bt->L=NULL; bt->R=NULL; print(bt->L,l1,l1+mid-l2-1,l2,mid-1); print(bt->R,l1+mid-l2,r1-1,mid+1,r2); } vector<int>p; void s(tree bt) { queue<tree>Q; Q.push(bt); while(!Q.empty()){ tree u=Q.front(); Q.pop(); p.push_back(u->data); if(u->L) Q.push(u->L); if(u->R) Q.push(u->R); } } int main() { int n; scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d",&post[i]); for(int i=0;i<n;i++) scanf("%d",&in[i]); tree bt; print(bt,0,n-1,0,n-1); s(bt); for(int i=0;i<p.size();i++){ if(i) printf(" "); printf("%d",p[i]); } printf(" "); return 0; }