注:BFS,重构二叉树
#include#include #include #include using namespace std; const int maxn=88; int post[maxn],pre[maxn],in[maxn]; struct node{ int data; node* lchild; node* rchild; }; node* create (int postL,int postR,int inL,int inR) { if (postL>postR) return NULL; node* root=new node; root->data=post[postR]; int k; for (k=inL;k<=inR;k++) if (post[postR]==in[k]) break; int numLeft=k-inL; root->lchild=create(postL,postL+numLeft-1,inL,k-1); root->rchild=create(postL+numLeft,postR-1,k+1,inR); return root; } int num=0,n; void BFS(node* root) { queue q; q.push(root); while (q.empty()==false) { node* now=q.front(); q.pop(); printf ("%d",now->data); num++; if (num lchild!=NULL) q.push(now->lchild); if (now->rchild!=NULL) q.push(now->rchild); } } int main () { int i; scanf ("%d",&n); for (i=0;i



