目录
写出代码前,得先弄懂原理。如果你还没有弄懂类似:“给定二叉树已知后序序列和中序序列,或已知前序序列和中序序列,画出二叉树” 这类问题,建议先看一下此文:二叉树已知后序序列和中序序列,或已知前序序列和中序序列,画出二叉树(简单易懂
一、已知二叉树后序和中序
描述:
假设二叉树上各结点的权值互不相同且都为正整数。
给定二叉树的后序遍历和中序遍历,请你输出二叉树的前序遍历和层序遍历
输入格式
第一行包含整数 N,表示二叉树结点总数。
第二行给出二叉树的后序遍历序列。
第三行给出二叉树的中序遍历序列。
1、求前序:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 111;
int n, postorder[maxn], inorder[maxn];
unordered_map<int, int> pos;
void build(int il, int ir, int pl, int pr){
int root = postorder[pr];
int k = pos[root];
cout << root << " ";
if(il < k) build(il, k - 1, pl, k - 1 - il + pl);
if(ir > k) build(k + 1, ir, k - il + pl, pr - 1);
}
int main(){
int n;
cin >> n;
for(int i = 0; i < n; i++) cin >> postorder[i];
for(int i = 0; i < n; i++){
cin >> inorder[i];
pos[inorder[i]] = i;
}
build(0, n - 1, 0, n - 1);
return 0;
}
2、求层序:
#include<bits/stdc++.h> //已知后中求层序
using namespace std;
const int maxn = 111;
int n, postorder[maxn], inorder[maxn];
unordered_map<int, int> l, r, pos;
int build(int il, int ir, int pl, int pr){
int root = postorder[pr];
int k = pos[root];
if(il < k) l[root] = build(il, k - 1, pl, k - 1 - il + pl);
if(ir > k) r[root] = build(k + 1, ir, k - il + pl, pr - 1);
return root;
}
void bfs(int root){
queue<int> q;
q.push(root);
while(!q.empty()){
int t = q.front();
q.pop();
cout << t << " ";
if(l.count(t)) q.push(l[t]);
if(r.count(t)) q.push(r[t]);
}
}
int main(){
cin >> n;
for(int i = 0; i < n; i++)
cin >> postorder[i];
for(int i = 0; i < n; i++){
cin >> inorder[i];
pos[inorder[i]] = i;
}
int root = build(0, n - 1, 0, n - 1);
bfs(root);
return 0;
}
二、已知二叉树前序和中序
描述:
假设二叉树上各结点的权值互不相同且都为正整数。
给定二叉树的前序遍历和中序遍历,请你输出二叉树的后序遍历和层序遍历
输入格式
第一行包含整数 N,表示二叉树结点总数。
第二行给出二叉树的前序遍历序列。
第三行给出二叉树的中序遍历序列。
1、求后序:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int n, preorder[maxn], inorder[maxn];
unordered_map<int, int> pos;
void build(int il, int ir, int pl, int pr){
int root = preorder[pl];
int k = pos[root];
if(il < k) build(il, k - 1, pl + 1, k - 1 - il + pl + 1);
if(ir > k) build(k + 1, ir, k - 1 - il + pl + 1 + 1, pr);
cout << root << " ";
}
int main(){
cin >> n;
for(int i = 0; i < n; i++) cin >> preorder[i];
for(int i = 0; i < n; i++){
cin >> inorder[i];
pos[inorder[i]] = i;
}
build(0, n - 1, 0, n - 1);
return 0;
}
2、求层序:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int n, preorder[maxn], inorder[maxn];
unordered_map<int, int> l, r, pos;
int build(int il, int ir, int pl, int pr){
int root = preorder[pl];
int k = pos[root];
if(il < k) l[root] = build(il, k - 1, pl + 1, k - 1 - il + pl + 1);
if(ir > k) r[root] = build(k + 1, ir, k - 1 - il + pl + 1 + 1, pr);
return root;
}
void bfs(int root){
queue<int> q;
q.push(root);
while(!q.empty()){
int t = q.front();
cout << t << " ";
q.pop();
if(l.count(t)) q.push(l[t]);
if(r.count(t)) q.push(r[t]);
}
}
int main(){
int n;
cin >> n;
for(int i = 0; i < n; i++) cin >> preorder[i];
for(int i = 0; i < n; i++){
cin >> inorder[i];
pos[inorder[i]] = i;
}
int root = build(0, n - 1, 0, n - 1);
bfs(root);
return 0;
}