(PAT 1127) ZigZagging on a Tree (二叉树建立+层序遍历)

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

解题思路:

先建树,建树过程参见模板

然后层序遍历,保留层号,把每一层的所有元素保留到对应的vector容器中

输出时,层号为奇数,逆序输出,层号为偶数,正序输出

#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int MANX = 35;
int postSquence[MANX], inSquence[MANX];
vector<int> nums[MANX];
struct Bnode {
	int data;
	Bnode* lchild;
	Bnode* rchild;
	Bnode(int _data) {
		data = _data;
		lchild = nullptr;
		rchild = nullptr;
	}
};

Bnode* BbuildTree(int postL, int postR, int inL, int inR) {
	if (postL > postR) return nullptr;
	int curPostRoot = postSquence[postR];
	//确定根的值
	Bnode* root = new Bnode(curPostRoot);
	int curInRoot = -1;
	int k;
	//在中序中查找根,确定根的位置
	for (k = inL; k <= inR; ++k) {
		if (inSquence[k] == curPostRoot) {
			curInRoot = inSquence[k];
			break;
		}
	}
	if (curInRoot == -1) return nullptr;
	//后序遍历
	int numleft = k - inL;
	root->lchild = BbuildTree(postL, postL + numleft - 1, inL, k - 1);
	root->rchild = BbuildTree(postL + numleft, postR - 1, k + 1, inR);

	return root;
}

int order = 0;
void LayerPrint(Bnode* root) {
	queue<Bnode*> bfs_queue;
	queue<int> bfs_orderQueue;
	bfs_queue.push(root);
	bfs_orderQueue.push(1);
	while (!bfs_queue.empty()) {
		Bnode* currentNode = bfs_queue.front();
		order = bfs_orderQueue.front();
		nums[order].push_back(currentNode->data);
		bfs_queue.pop();
		bfs_orderQueue.pop();
		if (currentNode->lchild != nullptr) {
			bfs_queue.push(currentNode->lchild);
			bfs_orderQueue.push(order + 1);
		}
		if (currentNode->rchild != nullptr) {
			bfs_queue.push(currentNode->rchild);
			bfs_orderQueue.push(order + 1);
		}
	}
}
int main() {
	int N;
	cin >> N;
	for (int i = 0; i < N; ++i) {
		cin >> inSquence[i];
	}
	for (int i = 0; i < N; ++i) {
		cin >> postSquence[i];
	}

	Bnode* root = BbuildTree(0, N - 1, 0, N - 1);
	LayerPrint(root);
	for (int i = 1; i <= order; ++i) {
		if (i % 2 != 0) {
			reverse(nums[i].begin(), nums[i].end());
			for (auto itr = nums[i].begin(); itr != nums[i].end(); ++itr) {
				cout << *itr;
				if (i != order || itr != nums[i].end() - 1) cout << " ";
			}
		}
		else {
			for (auto itr = nums[i].begin(); itr != nums[i].end(); ++itr) {
				cout << *itr;
				if (i != order || itr != nums[i].end() - 1) cout << " ";
			}
		}
	}

	system("PAUSE");
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值