原题描述
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.
中文描述
一个二叉树,树中每个节点的权值互不相同。 现在给出它的后序遍历和中序遍历,请你输出它的层序遍历。输入格式
第一行包含整数 N,表示二叉树的节点数。
第二行包含 N 个整数,表示二叉树的后序遍历。
第三行包含 N 个整数,表示二叉树的中序遍历。
输出格式
输出一行 N 个整数,表示二叉树的层序遍历。
Sample Input:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
Sample Output:
4 1 6 3 5 7 2
这题其实就是利用树的中序遍历还有后序遍历来重建二叉树(在这要说明一下如果是前序还有中序也可以重建二叉树,但是如果只有前序和后序就重建不了)然后利用bfs来进行层序遍历,这一题还是比较简单的不过有个贼坑的地方让我多花了30分钟😂:虽然这的N很小但是它可没说节点的编号是1~N,在这它的节点编号可以很大,所以存数据的时候要使用哈希映射,看代码。
之前的错误代码
错误的地方是在这(都怪我太天真…以为就是1~N):
int l[N],r[N];
int pos[N];
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
const int N = 35;
int inorder[N], postorder[N];
int n;
int l[N],r[N];
int pos[N]; //用于定位
vector<int>vv;
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, pl + (k - 1) - il);
if (ir > k)r[root] = build(k + 1, ir, pl + (k - il), pr - 1);
return root;
}
void bfs(int u) {
queue<int>qq;
qq.push(u);
while (!qq.empty()) {
int k = qq.front();
vv.push_back(k);
qq.pop();
if (l[k] != -1)qq.push(l[k]);
if (r[k] != -1)qq.push(r[k]);
}
}
int main() {
memset(l, -1, sizeof(l));
memset(r, -1, sizeof(r));
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);
//bfs层序遍历
bfs(postorder[n - 1]);
cout << vv[0];
for (int i = 1; i < n; i++) {
cout << " " << vv[i];
}
return 0;
}
错误的运行结果
正确代码
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
#include<unordered_map>
using namespace std;
const int N = 35;
int inorder[N], postorder[N];
int n;
//特别注意虽然N很小但是它的节点其实可以很大
unordered_map<int, int> l;
unordered_map<int, int>r;
unordered_map<int,int>pos;//用于定位
vector<int>vv;
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, pl + (k - 1) - il);
if (ir > k)r[root] = build(k + 1, ir, pl + (k - il), pr - 1);
return root;
}
void bfs(int u) {
queue<int>qq;
qq.push(u);
while (!qq.empty()) {
int k = qq.front();
vv.push_back(k);
qq.pop();
if (l.count(k)!=0)qq.push(l[k]);
if (r.count(k)!=0)qq.push(r[k]);
}
}
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;
}
//重建二叉树
build(0, n - 1, 0, n - 1);
bfs(postorder[n - 1]);
cout << vv[0];
for (int i = 1; i < n; i++) {
cout << " " << vv[i];
}
return 0;
}
运行结果
花了30多分钟才发现,以后要好好看题目了,错误发生的是触不及防呀😂,希望这篇文章对你有帮助o(^▽^)o