A1127 ZigZagging on a Tree (30分) 中序后序建树(***)

本题涉及到层序遍历和中序后序建树的问题;
关于z型输出,可以先存在vector中,利用层数的奇偶性来判断是正向输出还是逆向输出;

#include<cstdio>
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
const int maxn=40;
int n,in[maxn],post[maxn],maxl=-1,num=0;
struct node
{
	int data,layer;
	node *lchild,*rchild;
};
vector<int> ans[maxn];
node* getroot(int inl,int inr,int posl,int posr,int l)
{
	if(posl>posr)
	return NULL;
	node* root=new node;
	root->data=post[posr];
	root->layer=l;
	if(l>maxl)
	maxl=l;
	int i;
	for(i=inl;i<=inr;i++)
	{
		if(post[posr]==in[i])
		break;
	}
	root->lchild=getroot(inl,i-1,posl,posl+i-1-inl,l+1);
	root->rchild=getroot(i+1,inr,posl+i-inl,posr-1,l+1);
	return root;
}
void BFS(node* root)
{
	queue<node*> q;
	q.push(root);
	while(!q.empty())
	{
		node *temp=q.front();
		ans[temp->layer].push_back(temp->data);
		q.pop();
		if(temp->lchild!=NULL)q.push(temp->lchild);
		if(temp->rchild!=NULL)q.push(temp->rchild);
	}
	for(int i=1;i<=maxl;i++)
	{
		if(i%2==0)
		{
		  for(int j=0;j<ans[i].size();j++)
	      {
			printf("%d",ans[i][j]);
			num++;
			if(num!=n)
			printf(" ");
	   	  }
		}
		else
		{
			for(int j=ans[i].size()-1;j>=0;j--)
			{
				printf("%d",ans[i][j]);
				num++;
				if(num!=n)
				printf(" ");
			}
		}
	}
}
int main()
{
	cin>>n;
	for(int i=0;i<n;i++)
	{
		scanf("%d",&in[i]);
	}
	for(int i=0;i<n;i++)
	{
		scanf("%d",&post[i]);
	}
	node* Root=getroot(0,n-1,0,n-1,1);
	BFS(Root);
}
### Z字形遍历(Zigzag Traversal)的概念 二叉树的Z字形遍历是一种特殊的层遍历方式,在这种遍历中,每一层节点按照不同的方向访问。奇数层从左到右访问,偶数层则从右到左访问[^1]。 --- ### 实现思路 为了实现Z字形遍历,可以利用两个栈来别存储当前层和下一层的节点。通过交替操作这两个栈,可以在不改变原有树结构的情况下完成Z字形遍历。具体方法如下: - 使用一个标志变量 `left_to_right` 来控制每层的方向。 - 当前层的节点按指定方向弹出并处理,同时将其子节点按相反顺压入下一个栈中。 - 完成当前层后切换方向继续处理下一层次。 这种方法的时间复杂度为 O(n),其中 n 是树中的节点总数,因为每个节点仅被访问一次。 --- ### Zigzag 遍历的 C 语言实现 以下是基于上述逻辑编写的 C 语言代码示例: ```c #include <stdio.h> #include <stdlib.h> // 定义二叉树节点结构体 typedef struct TreeNode { int val; struct TreeNode* left; struct TreeNode* right; } TreeNode; // 创建新节点函数 TreeNode* createNode(int value) { TreeNode* newNode = (TreeNode*)malloc(sizeof(TreeNode)); newNode->val = value; newNode->left = NULL; newNode->right = NULL; return newNode; } void zigzagTraversal(TreeNode* root) { if (!root) return; // 如果根为空,则直接返回 // 初始化两个栈 typedef struct Stack { TreeNode** array; int top; int capacity; } Stack; Stack* stack1 = (Stack*)malloc(sizeof(Stack)); Stack* stack2 = (Stack*)malloc(sizeof(Stack)); stack1->capacity = stack2->capacity = 100; // 假设最大容量为100 stack1->array = (TreeNode**)malloc(stack1->capacity * sizeof(TreeNode*)); stack2->array = (TreeNode**)malloc(stack2->capacity * sizeof(TreeNode*)); stack1->top = stack2->top = -1; // 将根节点推入第一个栈 stack1->array[++stack1->top] = root; int leftToRight = 1; // 方向标记:1 表示从左到右,0 表示从右到左 while (stack1->top != -1 || stack2->top != -1) { if (leftToRight) { // 处理 stack1 中的节点 while (stack1->top != -1) { TreeNode* node = stack1->array[stack1->top--]; printf("%d ", node->val); if (node->left) stack2->array[++stack2->top] = node->left; if (node->right) stack2->array[++stack2->top] = node->right; } } else { // 处理 stack2 中的节点 while (stack2->top != -1) { TreeNode* node = stack2->array[stack2->top--]; printf("%d ", node->val); if (node->right) stack1->array[++stack1->top] = node->right; if (node->left) stack1->array[++stack1->top] = node->left; } } // 切换方向 leftToRight = !leftToRight; } } int main() { // 构建一棵简单的二叉树作为测试数据 TreeNode* root = createNode(1); root->left = createNode(2); root->right = createNode(3); root->left->left = createNode(4); root->left->right = createNode(5); root->right->left = createNode(6); root->right->right = createNode(7); printf("Zigzag Traversal of Binary Tree:\n"); zigzagTraversal(root); // 调用zigzagTraversal函数打印结果 return 0; } ``` --- ### 输出解释 对于上面构建的二叉树,其结构如下所示: ``` 1 / \ 2 3 / \ / \ 4 5 6 7 ``` 执行程后的输出将是: ``` Zigzag Traversal of Binary Tree: 1 3 2 4 5 6 7 ``` 这表明第一层从左至右读取,第二层从右往左读取,依此类推。 --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值