L2-011 玩转二叉树
给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列。所谓镜面反转,是指将所有非叶结点的左右孩子对换。这里假设键值都是互不相等的正整数。
输入格式:
输入第一行给出一个正整数N(≤30),是二叉树中结点的个数。第二行给出其中序遍历序列。第三行给出其前序遍历序列。数字间以空格分隔。
输出格式:
在一行中输出该树反转后的层序遍历的序列。数字间以1个空格分隔,行首尾不得有多余空格。
输入样例:
7
1 2 3 4 5 6 7
4 1 3 2 6 5 7
输出样例:
4 6 1 7 5 3 2
给定中序和前序遍历可以确定一棵树
层序就是bfs
#include <queue>
#include <vector>
#include <iostream>
using namespace std;
static const auto io_sync_off = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
return nullptr;
}();
const int maxn = 50;
//存放前序,中序遍历的结点
vector<int> fir(maxn), mid(maxn);
struct tree //存放树
{
int l, r;
} tree[maxn];
int buildTree(int fl, int fr, int ml, int mr) //先序,中序遍历的初始结束位置
{
if (ml > mr) //如果左子树的左大于右,说明没有子节点
return 0;
int root = fir[fl], p1 = ml, p2;
while (mid[p1] != root)
++p1; //找到根
p2 = p1 - ml; //左子树的个数
tree[root].l = buildTree(fl + 1, fl + p2, ml, p1 - 1); //递归建立左子树
tree[root].r = buildTree(fl + p2 + 1, fr, p1 + 1, mr); //递归建立右子树
return root;
}
void bfs(int r)
{
queue<int> q;
vector<int> ans;
q.push(r);
while (!q.empty())
{
int root = q.front();
q.pop();
if (root == 0)
break; //如果树空
ans.push_back(root);
if (tree[root].r) //要求镜面反转,所以先加右子树
q.push(tree[root].r);
if (tree[root].l)
q.push(tree[root].l);
}
for (int i = 0; i < ans.size(); ++i)
cout << ans[i] << " \n"[i == ans.size() - 1];
}
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; ++i)
cin >> mid[i];
for (int i = 0; i < n; ++i)
cin >> fir[i];
int root = buildTree(0, n - 1, 0, n - 1);
bfs(root);
return 0;
}