题目链接

模板

1.中序和前序还原二叉树
2.层序遍历
3.镜像层序遍历

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int pre[33], in[33];
vector<int> level,reverse;
struct node{
	int data;
	node* left, *right;
};

node* build_tree(int preL, int preR,int inL, int inR){
	if(preL > preR) return NULL;
	node* root = new node;
	root->data = pre[preL];
	int k=inL;
	while(in[k] != pre[preL]){
		k++;
	}
	int left = k - inL;
	
	root->left = build_tree(preL+1, preL+left,inL,k-1);
	root->right = build_tree(preL+left+1, preR,k+1, inR); 
	return root;
}

void levelorder(node* root){
	queue<node*> q;
	q.push(root);
	while(!q.empty()){
		node* now = q.front();
		q.pop();
		level.push_back(now->data);
		if(now->left!=NULL) q.push(now->left);
		if(now->right!=NULL) q.push(now->right); 
	}
}
void reverseorder(node* root){
	queue<node*> q;
	q.push(root);
	while(!q.empty()){
		node* now = q.front();
		q.pop();
		reverse.push_back(now->data);
		if(now->right!=NULL) q.push(now->right); 
		if(now->left!=NULL) q.push(now->left);
	}
}
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>in[i];
	}
	for(int i=0;i<n;i++){
		cin>>pre[i];
	}
	node * root = build_tree(0,n-1,0,n-1);
	reverseorder(root);
	for(int i=0;i<reverse.size();i++){
		cout<<reverse[i];
		if(i!=reverse.size()-1) cout<<" ";
		else cout<<endl;
	}
	return 0;
}