PAT甲级——1127 ZigZagging on a Tree (30 分)

1127 ZigZagging on a Tree (蛇形打印二叉树)

1127 ZigZagging on a Tree (30 分)

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.

zigzag.jpg

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 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

题目大意:给定二叉树的中序和后序遍历的数列,蛇形逐层打印二叉树。

思路:中序+后序,建立二叉树;(建树的具体实现方法和思路见我的博客园文章:后序+中序,先序+中序,建立二叉树)建树的过程顺便记录每个节点所处的深度以及树的总深度;接着层序遍历,将每个节点的所属层数对应二维数组ans的行,依次push back~~最后将ans蛇形输出,奇数层(我是从0开始计数的)从左往右,偶数层反之。

#include<iostream>
#include<vector>
#include<unordered_map>
#include<queue>
using namespace std;
typedef struct node* BT;
struct node {
	int value, level = 0;
	BT left = NULL, right = NULL;
};
int treeDepth = 0;//树的深度 
unordered_map<int, int> mp;//映射元素在中序数组里的位置 
vector<int> Inorder, Post, ans[30];
BT buildTree(int InLeft, int InRight, int PostIndex, int depth);
void levelOrder(BT t);
void printLtoR(vector<int> &v);
void printRtoL(vector<int> &v);
int main()
{
	int N, i;
	scanf("%d", &N);
	Inorder.resize(N);
	Post.resize(N);
	for (i = 0; i < N; i++) {
		scanf("%d", &Inorder[i]);
		mp[Inorder[i]] = i;
	}
	for (i = 0; i < N; i++)
		scanf("%d", &Post[i]);
	BT tree = NULL;
	tree = buildTree(0, N - 1, N - 1, 0);
	levelOrder(tree);
	for (i = 0; i <= treeDepth; i++) {
		if (i % 2 == 0) printRtoL(ans[i]);
		else printLtoR(ans[i]);
		if (i <= treeDepth - 1)
			printf(" ");
	}
	return 0;
}
void printRtoL(vector<int> &v)
{
	int n = v.size();
	for (int i = n - 1; i >= 0; i--) {
		printf("%d", v[i]);
		if (i > 0)
			printf(" ");
	}
}
void printLtoR(vector<int> &v)
{
	int n = v.size();
	for (int i = 0; i < n; i++) {
		printf("%d", v[i]);
		if (i < n - 1)
			printf(" ");
	}
}
void levelOrder(BT t)
{
	queue<BT> Q;
	Q.push(t);
	while (!Q.empty()) {
		BT tmp = Q.front();
		ans[tmp->level].push_back(tmp->value);
		Q.pop();
		if (tmp->left)
			Q.push(tmp->left);
		if (tmp->right)
			Q.push(tmp->right);
	}
}
BT buildTree(int InLeft, int InRight, int PostIndex, int depth)
{
	if (InLeft > InRight) return NULL;
	if (treeDepth < depth) treeDepth = depth;
	BT t = new node();
	t->level = depth;
	t->value = Post[PostIndex];
	t->right = buildTree(mp[Post[PostIndex]] + 1, InRight, PostIndex - 1, depth + 1);
	int rtreeNum = InRight - mp[Post[PostIndex]];//当前节点的右子树节点个数 
	t->left = buildTree(InLeft, mp[Post[PostIndex]] - 1, PostIndex - rtreeNum - 1, depth + 1);
	return t;
}

猜你喜欢

转载自blog.csdn.net/weixin_44385565/article/details/89091985