面试算法 面试算法 二叉树的最小深度 (简单的BFS)

1.题目:二叉树的最小深度 
给定一个二叉树,找出其最小深度,最小深度是从根节点到最近叶子节点的最短路径上的节点数量
广度优先。


2.算法:广度优先搜索(BFS)

广度优先搜索(也称宽度优先搜索,缩写BFS,以下采用广度来描述)是连通图的一种遍历策略。因为它的思想是从一个顶点V0V0开始,辐射状地优先遍历其周围较广的区域,因此得名。

一般可以用它做什么呢?一个最直观经典的例子就是走迷宫,我们从起点开始,找出到终点的最短路程,很多最短路径算法就是基于广度优先的思想成立的。


3.算法思想

广度优先搜索(BFS)的搜索思想是先确定二叉树中的根节点为起始节点,然后从这个起始点开始搜索和它左右子树 的每个点,然后再以这些搜索到的相邻的节点向外搜索和他们左右子树的其他节点,每个搜索过的节点都要进行标记,标记过的节点就不再搜索,每个节点都有自己距离起始节点的长度,这个长度以与他左右子树的上一个节点的长度+1;搜索直到所有可搜索的节点全部遍历,得到所有节点到起始节点的距离。

这里说明:他的根的深度 是 1              其他的子节点的深度  都是父节点的深度数+1。

  1. 把起始节点插入队列;
  2. 每次从队列的头部取出一个元素,查看这个元素所有的下一级相邻元素,把它们放到队列的末尾。并把这个元素记为它下一级元素的前驱;
  3. 找到要找的元素时结束,或者队列为空即所有元素全部遍历完

5.代码:

/*************************************************
作者:She001
时间:2022/8/29
题目:二叉树的最小深度 
给定一个二叉树,找出其最小深度
最小深度是从根节点到最近叶子节点的最短路径上的节点数量
广度优先、

算法:1.BFS 算法  (深度优先) 
BFS 需要用到 队列 所以需要写队列的 入队列 ,出队列
 
 	 

***************************************************/
#include<bits/stdc++.h>
using namespace std;
typedef struct student
{
	int a;
	struct student * left;
	struct student * right;
	int shendu;//树的深度 
}node;  //指针类型的 
/*//二叉树模型 
						
					a1
			a2              a3
		a4	  a5        a6     a7
		            a8
                       a9
*/
///

/*
queue 操作
queue 和 stack 有一些成员函数相似,但在一些情况下,工作方式有些不同:
front():返回 queue 中第一个元素的引用。如果 queue 是常量,就返回一个常引用;如果 queue 为空,返回值是未定义的。
back():返回 queue 中最后一个元素的引用。如果 queue 是常量,就返回一个常引用;如果 queue 为空,返回值是未定义的。
push(const T& obj):在 queue 的尾部添加一个元素的副本。这是通过调用底层容器的成员函数 push_back() 来完成的。
push(T&& obj):以移动的方式在 queue 的尾部添加元素。这是通过调用底层容器的具有右值引用参数的成员函数 push_back() 来完成的。
pop():删除 queue 中的第一个元素。
size():返回 queue 中元素的个数。
empty():如果 queue 中没有元素的话,返回 true。
emplace():用传给 emplace() 的参数调用 T 的构造函数,在 queue 的尾部生成对象。
swap(queue<T> &other_q):将当前 queue 中的元素和参数 queue 中的元素交换。它们需要包含相同类型的元素。也可以调用全局函数模板 swap() 来完成同样的操作。

*/

//建立队列
//算法: BFS 
int BFS(node * proot)
{
	if(proot==NULL)
	{
		return 0;
	}
	queue<node*>q;
	proot->shendu=1; 
	q.push(proot);
	while(!q.empty())
	{
		node * hh=q.front();
		if(hh ->left!=NULL)
		{
			hh->left->shendu=hh->shendu+1;
			q.push(hh->left);
		}
		if(hh->right!=NULL)
		{
			hh->right->shendu=hh->shendu+1; 
			q.push(hh->right);
		}
		if(hh->right==NULL && hh->left==NULL)
		{
			return hh->shendu;
		}
		q.pop();
	}
	cout<<endl;
}






int main()
{
	//数据初始化,建立树 
	struct student *a1 =new struct student; 
	struct student *a2 =new struct student; 
	struct student *a3 =new struct student; 
	struct student *a4 =new struct student; 
	struct student *a5 =new struct student; 
	struct student *a6 =new struct student; 
	struct student *a7 =new struct student; 
	struct student *a8 =new struct student; 
	struct student *a9 =new struct student; 
	//数值的赋值 
	a1->a=1; 
	a2->a=2; 
	a3->a=3; 
	a4->a=4; 
	a5->a=5; 
	a6->a=6; 
	a7->a=7; 
	a8->a=8; 
	a9->a=9; 
	//节点的连接 
	a1->left=a2;
	a1->right=a3;
	a2->left=a4;
	a2->right=a5;
	a3->left=a6;
	a3->right=a7;
	a6->left=a8;
	a8->right=a9;
	//节点为空的设置
	a4->left=NULL;
	a4->right=NULL;
	a5->left=NULL;
	a5->right=NULL;
	a8->left=NULL;
	a9->left=NULL;
	a9->right=NULL;
	a6->right=NULL;
	a7->left=NULL;
	a7->right=NULL;
	 
	
	int a=BFS(a1);
	cout<<"最小深度: "<<a<<endl;
		
	
	
	//释放空间 
	delete a1;
	delete a2;
	delete a3;
	delete a4;
	delete a5;
	delete a6;
	delete a7;
	delete a8;
	delete a9;	
	  
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值