数据结构入门-二叉树的层序遍历

如何判断一棵二叉树的深度?

层序遍历

过程演示

使用队列完成层序遍历:

1.定义变量深度depth=0,树根入队即第一层入队

2.树根出队,depth+1

3.树根的左右子树也就是第二层入队

4.B节点出队,B节点的左右子树入队

5.C节点出队,depth+1,C节点的左右子树入队

以此循环,求出二叉树总深度depth。

代码实现

cpp 复制代码
//获取二叉树深度
int maxDepth(TreeNode* root)
{
	if (root == NULL)
	{
		return 0;
	}

	int depth = 0;//深度
	Queue* q = initQueue();//初始化队列
	equeue(q, root);//树根入队

	while (!isEmpty(q))
	{
		int count = queueSize(q);
		while (count > 0)
		{
			TreeNode* curr;
			dequeue(q, &curr);
			if (curr->lchild != NULL)
			{
				equeue(q, curr->lchild);
			}
			if (curr->rchild != NULL)
			{
				equeue(q, curr->rchild);
			}
			count--;
		}
		depth++;
	}
	return depth;
}

主程序

这个程序中求二叉树深度的实现依靠队列等知识点

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
const int MAXSIZE =100;
typedef char TreeType;

//树的结构
typedef struct TreeNode
{
	TreeType data;
	struct TreeNode* lchild;
	struct TreeNode* rchild;
}TreeNode;

typedef TreeNode* ElemType;

//队列结构
typedef struct
{
	ElemType* data;
	int front;
	int rear;
}Queue;

typedef TreeNode* BiTree;

char str[] = "ABDH#K###E##CFI###G#J##";
int index = 0;

void createTree(BiTree *T)
{
	TreeType ch;
	ch = str[index++];
	if (ch == '#')
	{
		*T=NULL;
	}
	else
	{
		*T = new TreeNode;
		(*T)->data = ch;
		createTree(&(*T)->lchild);
		createTree(&(*T)->rchild);
	}
}
//初始化
Queue* initQueue()
{
	Queue* q = new Queue;
	q->data = new ElemType[MAXSIZE];
	q->front = 0;
	q->rear = 0;
	return q;

}

//判断队列是否为空
int isEmpty(Queue* Q)
{
	if (Q->front == Q->rear)
	{
		cout << "空的" << endl;
		return 1;
	}
	else
	{
		return 0;
	}
}

//入队
int equeue(Queue* Q, ElemType e)
{
	if ((Q->rear + 1) % MAXSIZE == Q->front)
	{
		cout << "满了" << endl;
		return 0;
	}
	Q->data[Q->rear] = e;
	Q->rear = (Q->rear + 1) % MAXSIZE;
	return 1;


}

//出队
int dequeue(Queue* Q, ElemType* e)
{
	if (Q->front == Q->rear)
	{
		cout << "空的" << endl;
		return 0;
	}
	*e = Q->data[Q->front];
	Q->front = (Q->front + 1) % MAXSIZE;
	return 1;
}
//获取队头元素
int getHead(Queue* Q, ElemType* e)
{
	if (Q->front == Q->rear)
	{
		cout << "空的" << endl;
	}
	*e = Q->data[Q->front];
	return 1;
}

//获取队列元素数量
int queueSize(Queue* Q)
{
	if (!isEmpty(Q))
	{
		return (Q->rear - Q->front+MAXSIZE)%MAXSIZE;
	}
	else
	{
		return 0;
	}
}

//获取二叉树深度
int maxDepth(TreeNode* root)
{
	if (root == NULL)
	{
		return 0;
	}

	int depth = 0;//深度
	Queue* q = initQueue();//初始化队列
	equeue(q, root);//树根入队

	while (!isEmpty(q))
	{
		int count = queueSize(q);
		while (count > 0)
		{
			TreeNode* curr;
			dequeue(q, &curr);
			if (curr->lchild != NULL)
			{
				equeue(q, curr->lchild);
			}
			if (curr->rchild != NULL)
			{
				equeue(q, curr->rchild);
			}
			count--;
		}
		depth++;
	}
	return depth;
}


int main()
{
	BiTree T;
	createTree(&T);
	cout << maxDepth(T) << endl;

	return 0;
}

运行程序可获得该二叉树深度为5。

相关推荐
海洲探索-Hydrovo3 小时前
TTP Aether X 天通透传模块丨国产自主可控大数据双向通讯定位模组
网络·人工智能·科技·算法·信息与通信
_OP_CHEN4 小时前
C++基础:(十二)list类的基础使用
开发语言·数据结构·c++·stl·list类·list核心接口·list底层原理
2401_841495646 小时前
【计算机视觉】基于复杂环境下的车牌识别
人工智能·python·算法·计算机视觉·去噪·车牌识别·字符识别
Jonkin-Ma6 小时前
每日算法(1)之单链表
算法
晚风残7 小时前
【C++ Primer】第六章:函数
开发语言·c++·算法·c++ primer
杨云强7 小时前
离散积分,相同表达式数组和公式
算法
地平线开发者7 小时前
征程 6 | BPU trace 简介与实操
算法·自动驾驶
满天星83035777 小时前
【C++】AVL树的模拟实现
开发语言·c++·算法·stl
Lris-KK8 小时前
力扣Hot100--94.二叉树的中序遍历、144.二叉树的前序遍历、145.二叉树的后序遍历
python·算法·leetcode
麦麦鸡腿堡8 小时前
Java的动态绑定机制(重要)
java·开发语言·算法