【笨方法学PAT】1127 ZigZagging on a Tree (30 分)

版权声明:欢迎转载,请注明来源 https://blog.csdn.net/linghugoolge/article/details/84284692

一、题目

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

二、题目大意

中序后序建树,正反序层次遍历。

三、考点

TREE

四、注意

1、后序中序建树;

2、queue层序遍历无法保存深度信息,使用dfs保存深度,vector<int> dep[N]

五、代码

#include<iostream>
#include<vector>
#include<queue>
#define N 32
using namespace std;

struct node {
	int val;
	struct node *left, *right;
};
vector<int> in, post,level,pre,zig;
vector<int> dep[N];
int max_dep;

node* buildTree(int pl, int pr, int il, int ir) {
	if (pl > pr)
		return NULL;
	node *root = new node();
	root->val = post[pr];
	root->left = root->right = NULL;

	//pre-order
	pre.push_back(post[pr]);

	//child-tree
	int index = il;
	while (index <= ir && in[index] != post[pr])
		index++;
	root->left = buildTree(pl, index-1-il+pl, il, index - 1);
	root->right = buildTree(pr-1-(ir-index-1), pr - 1, index + 1, ir);

	return root;
}

void dfs(node *root,int depth) {
	if (root == NULL)
		return;

	dep[depth].push_back(root->val);
	if (depth > max_dep)
		max_dep = depth;

	//next
	if (root->left != NULL)
		dfs(root->left, depth + 1);
	if (root->right != NULL)
		dfs(root->right, depth + 1);
}

int main() {
	//read
	int n;
	cin >> n;
	in.resize(n), post.resize(n);
	for (int i = 0; i < n; ++i) 
		cin >> in[i];
	for (int i = 0; i < n; ++i)
		cin >> post[i];

	//buildTree
	node *root = buildTree(0, n - 1, 0, n - 1);

	//level-order
	queue<node*> que;
	que.push(root);
	while (!que.empty()) {
		node *root = que.front();
		level.push_back(root->val);
		que.pop();
		if (root->left != NULL)
			que.push(root->left);
		if (root->right != NULL)
			que.push(root->right);
	}

	/*for (int i = 0; i < level.size(); ++i) {
		if (i != 0)
			cout << " ";
		cout << level[i];
	}*/

	//zig level-order
	dfs(root, 1);
	cout << dep[1][0];
	for (int i = 2; i <= max_dep; ++i) {
		if (i % 2 == 0) {
			for (int j = 0; j < dep[i].size(); ++j) {
				cout <<" "<< dep[i][j];
			}
		}
		else {
			for (int j = dep[i].size()-1; j >= 0; --j) {
				cout << " " << dep[i][j];
			}
		}
	}

	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/linghugoolge/article/details/84284692