1020 Tree Traversals (25 分)
给定二叉树中序和后序写出层序遍历结果
对于二叉树遍历,中序,前序或者中序,后序或者中序,层序可以构建一个独一无二的二叉树,其他情况不行
本题代码:
#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
typedef int type;
const int N = 1e5 + 5;
type in[N],post[N];
vector<int> ans;
struct node{
type data;
node* letf;
node* right;
int layer;
};
node* post_create(int inL, int inR, int postL, int postR) {
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 numL = k - inL;
root->letf = post_create(inL, k-1, postL, postL+numL-1);
root->right = post_create(k+1, inR, postL+numL, postR-1);
return root;
}
void layerorder(node* root) {
queue<node*> q;
q.push(root);
while(!q.empty()) {
node* now = q.front();
q.pop();
// cout<<now->data<<" " ;
ans.push_back(now->data);
if(now->letf != NULL) q.push(now->letf);
if(now->right != NULL) q.push(now->right);
}
}
int main() {
int n;
cin >> n;
for(int i = 1; i <= n; i++) {
scanf("%d",post+i);
}
for(int i = 1; i <= n; i++) {
scanf("%d",in+i);
}
node* tree = post_create(1, n, 1, n);
layerorder(tree);
for(int i = 0; i < n-1; i++) cout<<ans[i]<<" ";
cout<<ans[n-1]<<endl;
return 0;
}
板子代码如下(都是递归实现):
#include<bits/stdc++.h>
using namespace std;
typedef int type;
const int N = 1e5 + 5;
int n; //结点数
vector<type> ans;//遍历结果
//二叉树的存储结构实现可以用指针也可以用数组(完全二叉树建议使用数组)
struct node{
type data;
node *l, *r;
int layer;
};
void preorder(node *root) {//前序遍历
if(root == NULL) return;
cout<<root->data<<" ";//不需要格式处理这样既可,需要格式处理用vector存储
// ans.push_back(root->data);
preorder(root->l);
preorder(root->r);
}
void inorder(node* root) {//中序遍历
if(root == NULL) return;
inorder(root->l);
cout<<root->data<<" ";
// ans.push_back(root->data);
inorder(root->r);
}
void postorder(node* root) {//后序遍历
if(root == NULL) return;
postorder(root->l);
postorder(root->r);
cout<<root->data<<" ";
// ans.push_back(root->data);
}
void layerorder(node* root) {//层序遍历
queue<node*> q;
q.push(root);
while(q.size()) {
node* top = q.front(); q.pop();
cout<<top->data<<" " ;
// ans.push_back(top->data);
if(top->l) {
top->l->layer = top->layer+1;//如果要计算层次
q.push(top->l);
}
if(top->r) {
top->r->layer = top->layer+1;
q.push(top->r);
}
}
}
int pre[N], in[N], post[N];
vector<int> cen;//重建树input:前,中,后,层序
node* create_pre(int preL, int preR, int inL, int inR) {//通过前中序重建树
if(preL > preR) return NULL;
node* root = new node;
root->data = pre[preL];
int k = inL;
for(; k <= inR; k++) {
if(in[k] == pre[preL]) break;
}
int leftnum = k - inL;//左子树结点数
root->l = create_pre(preL+1, preL+leftnum, inL, k-1);
root->r = create_pre(preL+leftnum+1, preR, k+1, inR);
return root;
}
node* create_post(int inL, int inR, int postL, int postR) {//通过中后序重建树
if(postL > postR) return NULL;
node* root = new node;
root->data = post[postR];
int k = inL;
for(; k <= inR; k++) {
if(in[k] == post[postR]) break;
}
int numleft = k - inL;
root->l = create_post(inL, k-1, postL, postL+numleft-1);;
root->r = create_post(k+1, inR, postL+numleft, postR-1);
return root;
}
node* create_layer(vector<int> level, int l, int r) {
if(level.size() == 0) return NULL;
node* root = new node;
root->data = level[0];
vector<int> left, right;
bool f[100] = {0};
int k = l;
for(; k <= r; k++) {
f[in[k]] = 1;
if(in[k] == level[0]) break;
}
for(int i = 1; i < level.size(); i++) {
if(f[level[i]]) left.push_back(level[i]);
else right.push_back(level[i]);
}
root->l = create_layer(left, l, k-1);
root->r = create_layer(right, k+1, r);
return root;
}
int main() {
scanf("%d",&n);
// for(int i = 0; i < n; i++) scanf("%d",pre+i);
// for(int i = 0; i < n; i++) scanf("%d", post+i);
for(int i = 0; i < n; i++) scanf("%d",in+i);
for(int i = 0; i < n; i++) {
int tem;
scanf("%d",&tem);
cen.push_back(tem);
}
// node* root = create_pre(0, n-1, 0, n-1);
// node* root = create_post(0, n-1, 0, n-1);
node* root = create_layer(cen, 0, n-1);
printf("\n先序遍历:");
preorder(root);
printf("\n中序遍历:");
inorder(root);
printf("\n后序遍历:");
postorder(root);
printf("\n层序遍历:");
layerorder(root);
return 0;
}
/*
7
前中后层
4 1 3 2 6 5 7
1 2 3 4 5 6 7
2 3 1 5 7 6 4
4 1 6 3 5 7 2
*/