后序、中序得前序、层序
输入
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
输出
4 1 6 3 5 7 2
两种方法:
1.第一种编程较为复杂,但是思路清晰,而且通用。
建树+BFS
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=50;
struct node{
int data;
node* lchild;
node* rchild;
};
int pre[maxn],in[maxn],post[maxn];
int n;
node* create(int postL,int postR,int inL,int inR )
{
if(postL>postR || inL>inR)
{
return NULL;
}
node* root = new node;
root->data=post[postR];
int k=-1;
for(int i=inL;i<=inR;i++)
{
if(in[i]==post[postR])
{
k=i;
break;
}
}
int num_left=k-inL;
root->lchi