作者:Dfsk刘海_368 | 来源:互联网 | 2023-09-23 18:34
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple standard routine to print the numbers in level-order. However, if you think the problem is too simple, then you are too naive. This time you are supposed to print the numbers in "zigzagging order" -- that is, starting from the root, print the numbers level-by-level, alternating between left to right and right to left. For example, for the following tree you must output: 1 11 5 8 17 12 20 15.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<&#61; 30), the total number of nodes in the binary tree. The second line gives the inorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print the zigzagging sequence of the tree in a line. 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.
Sample Input:
8
12 11 20 17 1 15 8 5
12 20 17 11 15 8 5 1
Sample Output:
1 11 5 8 17 12 20 15
题目大意&#xff1a;给出一个树的中序和后序遍历结果&#xff0c;求它的Z字型层序遍历&#xff0c;也就是偶数层从右往左&#xff0c;奇数层从左往右遍历&#xff5e;
分析&#xff1a;分为3步&#xff1a;1.根据中序和后序建树 保存在tree二维数组中&#xff0c;比如&#xff1a;tree[i][0] &#61; val表示post[i]的左孩子是post[val]&#xff0c;tree[i][1] &#61; val表示post[i]的右孩子是post[val]&#xff5e;
2.进行广度优先搜索&#xff0c;将树从根结点开始所有结点层序遍历&#xff0c;保存在result二维数组中&#xff0c;比如&#xff1a;result[i]保存第i层所有结点的序列&#xff5e;
3.进行z字型输出&#xff0c;根据当前层号的奇偶性分别从左往右、从右往左遍历输出&#xff5e;
1. dfs&#xff1a;因为post(后序)是按照左、右、根的顺序遍历的&#xff0c;所以从右往左&#xff0c;最右边的肯定是根结点&#xff5e;所以postRight是当前子树的根结点的下标&#xff0c;将它的赋值给index&#xff0c;并继续dfs tree[index][0]和tree[index][1]&#xff5e;
根据post[postRight]的结点在in里面的下标位置i&#xff0c;可以得到i的左边是左子树&#xff0c;即inLeft 到 i - 1&#xff0c;右边是右子树&#xff1a;i &#43; 1 到 inRight。而对于post来说&#xff0c;根据左子树的结点个数i - inLeft可以得到[postLeft, postLeft &#43; (i - inLeft) - 1]是post中左子树的范围&#xff0c;[postLeft &#43; (i - inLeft), postRight - 1]是post中右子树的范围&#xff5e;
2.广度优先搜索&#xff0c;采用队列q&#xff0c;q中保存的是node结点&#xff0c;node.index表示当前节点在post中的下标&#xff0c;node.depth表示当前结点在树中的层数&#xff5e;
3.当 i % 2 &#61;&#61; 0的时候倒序输出&#xff0c;否则正序输出&#xff5e;
#include
#include
#include
using namespace std;
vector in, post, result[35];
int n, tree[35][2], root;
struct node {
int index, depth;
};
void dfs(int &index, int inLeft, int inRight, int postLeft, int postRight) {
if (inLeft > inRight) return;
index &#61; postRight;
int i &#61; 0;
while (in[i] !&#61; post[postRight]) i&#43;&#43;;
dfs(tree[index][0], inLeft, i - 1, postLeft, postLeft &#43; (i - inLeft) - 1);
dfs(tree[index][1], i &#43; 1, inRight, postLeft &#43; (i - inLeft), postRight - 1);
}
void bfs() {
queue q;
q.push(node{root, 0});
while (!q.empty()) {
node temp &#61; q.front();
q.pop();
result[temp.depth].push_back(post[temp.index]);
if (tree[temp.index][0] !&#61; 0)
q.push(node{tree[temp.index][0], temp.depth &#43; 1});
if (tree[temp.index][1] !&#61; 0)
q.push(node{tree[temp.index][1], temp.depth &#43; 1});
}
}
int main() {
cin >> n;
in.resize(n &#43; 1), post.resize(n &#43; 1);
for (int i &#61; 1; i <&#61; n; i&#43;&#43;) cin >> in[i];
for (int i &#61; 1; i <&#61; n; i&#43;&#43;) cin >> post[i];
dfs(root, 1, n, 1, n);
bfs();
printf("%d", result[0][0]);
for (int i &#61; 1; i <35; i&#43;&#43;) {
if (i % 2 &#61;&#61; 1) {
for (int j &#61; 0; j printf(" %d", result[i][j]);
} else {
for (int j &#61; result[i].size() - 1; j >&#61; 0; j--)
printf(" %d", result[i][j]);
}
}
return 0;
}