A1020. Tree Traversals(25)

这是一题二叉树遍历的典型题,告诉我们中序遍历和另外一种遍历序列,然后求任何一种遍历序列。
这题的核心

  1. 建树
  2. BFS
#include<bits/stdc++.h>
using namespace std;
const int MAXN=35;
int post[MAXN];
int in[MAXN];
int pre[MAXN];
int n;
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(in[k]==post[postR])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;
void BFS(node* root){
    queue<node*>q;
    q.push(root);
    while(!q.empty()){
        node* now=q.front();
        q.pop();
        printf("%d",now->data);
        num++;
        if(num<n)printf(" ");
        if(now->lchild!=NULL)q.push(now->lchild);
        if(now->rchild!=NULL)q.push(now->rchild);
    }
}
int main(){
    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);
    }
    node* root=create(0,n-1,0,n-1);
    BFS(root);
    return 0;
}
keep going
原文地址:https://www.cnblogs.com/MarkKobs-blog/p/10570036.html