二叉树的递归、层次建立/遍历和其递归/层次镜像

本文介绍了二叉树的节点定义,并详细讲解了如何使用递归和层次方法创建二叉树。接着,阐述了二叉树的三种递归遍历方式:前序、中序和后序,以及层次遍历的方法。最后,讨论了如何通过递归和层次策略实现二叉树的镜像操作。
摘要由CSDN通过智能技术生成

一. 二叉树的节点代码

struct BiNode {
	int data;
	BiNode* rchild, * lchild;
};
typedef BiNode* BiTree;

二. 二叉树的创建

2.1 递归创建

void CreateBitreeRecur(BiTree* T)
{
	int ch;
	scanf("%d", &ch);
	if (ch == 0)
	{
		*T = NULL;
	}
	else
	{
		*T = new(BiNode);
		(*T)->data = ch;
		CreateBitreeRecur(&(*T)->lchild);
		CreateBitreeRecur(&(*T)->rchild);
	}
}

2.2 层次创建

BiTree CreateBiTree(int a[], int n)
{
	deque<BiNode*> que;
	BiNode* p;
	BiNode* root;
	int front = 1, rear = 1;
	for (int i = 0; i <n; i++) //建立节点,将节点存入队列
	{
		if (a[i] == 0)
		{
			p = NULL;
		}
		else
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值