PAT (Advanced Level) Practice 1127 ZigZagging on a Tree (30分) (中序后序建树+层序遍历+栈处理)

1.题目

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

2.题目分析

1.中序后序遍历

其实与中序先序遍历差不多,只不过后序是左右根,所以要在后序中从后往前一个一个在中序遍历中找根节点

tree create(tree t, int root, int inl, int inr,int level )
{
	if (inl > inr)return NULL;
	if (t == NULL)
		t = (tree)malloc(sizeof(struct node));
		t->left = t->right = NULL;
	t->data = post[root];
	t->level = level;
	int i = inl;
	while (in[i] != post[root])i++;
	if (i <= inr)
	{
		t->right = create(t->right, root-1, i + 1, inr, level + 1);
		t->left = create(t->left, root-(inr-i)-1, inl, i - 1, level + 1);
//这里的root-(inr-i)-1:(inr-i)是右子树的个数,从后面的根节点位置向前移动(inr-i),再向前移动一个位置是之前根节点占据的位置,所以是root-(inr-i)-1
	}
	return t;
}

2.层序遍历+栈处理

在建树的时候加入了层数的设置,之后建立一个树的结构体指针数组,分别将节点放入,而如果节点层数是奇数且不是根节点的话(根节点层数为1)就压栈,当遇到偶数层数的节点先将栈中节点输出,再输出偶数节点(最后还要看一下栈中是不是还有元素,可能最后一个节点在奇数栈导致栈中元素未输出)

3.代码

#include<iostream>
#include<stack>
using namespace std;
typedef struct node *tree;
int in[31], post[31];
int n;
struct node
{
	int data;
	tree left;
	tree right;
	int level;
};
tree create(tree t, int root, int inl, int inr,int level )
{
	if (inl > inr)return NULL;
	if (t == NULL)
		t = (tree)malloc(sizeof(struct node));
		t->left = t->right = NULL;
	t->data = post[root];
	t->level = level;
	int i = inl;
	while (in[i] != post[root])i++;
	if (i <= inr)
	{
		t->right = create(t->right, root-1, i + 1, inr, level + 1);
		t->left = create(t->left, root-(inr-i)-1, inl, i - 1, level + 1);
	}
	return t;
}
bool space = false;
void LevelorderTraversal(tree t)
{
	stack<int>out;
	if (t == NULL)
		return;
	tree binTree[100];
	int head = 0, last = 0; 
	binTree[last++] = t;

	while (head < last)
	{
		tree temp = binTree[head++];
		if (temp->level % 2 == 1 && temp->level != 1)
			out.push(temp->data);
		else
		{
			while (!out.empty())
			{
				int tt = out.top(); out.pop(); printf(" %d", tt);
			}
			printf("%s%d", space == true ? " " : "", temp->data); space = true;
		}
		if (temp->left)
			binTree[last++] = temp->left;
		if (temp->right)
			binTree[last++] = temp->right;
	}
	while (!out.empty())
	{
		int tt = out.top(); out.pop(); printf(" %d", tt);
	}

}
int main()
{

	scanf("%d", &n);
	for (int i = 0; i < n; i++)
		scanf("%d", &in[i]);
	for (int i = 0; i < n; i++)
		scanf("%d", &post[i]);
	tree t = NULL;
	t=create(t, n-1, 0, n - 1,1);
	LevelorderTraversal(t);
}
发布了204 篇原创文章 · 获赞 4 · 访问量 5689

猜你喜欢

转载自blog.csdn.net/qq_42325947/article/details/105238850