题目:

Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

思路1:利用递归遍历,求最小深度

 //递归遍历求最小深度
class Solution {
public:
int run(TreeNode *root) {
if(root ==NULL)
return ;
int l = run(root->left);
int r = run(root->right);
if(l== || r==)
return l+r+;
return min(l, r) + ;
}
};

思路2:利用队列采用层序遍历,一旦找到一个叶节点,它肯定是最短的。

参考链接:

二叉树的层序遍历算法:https://blog.csdn.net/qq_29542611/article/details/79372678

 class Solution {
public:
int minDepth(TreeNode* root) {
if(root == NULL) return ; //处理边界情况,空树 queue<TreeNode*> q; //辅助数据结构-队列,用于层序遍历
TreeNode* now = root; //当前考察的节点,初始值为root
TreeNode* last = root; //所在层的最后一个节点,初始值为root
int level = ; //返回值最小深度,初始值为1
int size = ; //用于确定是否到达叶子节点 q.push(root); //根压入队列
while(!q.empty())
{
now = q.front();
q.pop();
size = q.size(); //记录队列长度 //左右孩子非空,压入队列
if(now->left != NULL)
q.push(now->left);
if(now->right != NULL)
q.push(now->right); //如果没有左右孩子入队,长度没有变化,说明这个节点是叶子节点,找到叶子节点,退出循环
if(size == q.size())
break; //检查是否到达当前层最后一个节点,
if(last == now)
{
level++;
if(!q.empty())
//last指向下一层的最后一个节点
last = q.back();
}
}
return level;
}
};

C++队列Queue类成员函数如下:

back()返回最后一个元素

empty()如果队列空则返回真

front()返回第一个元素

pop()删除第一个元素

push()在末尾加入一个元素

size()返回队列中元素的个数

总结:

二叉树操作主要还是利用尾递归或者循环遍历这两种思路,进而涉及DFS(主要利用递归或者栈实现)或者BFS(主要利用队列实现)。剩下的只需要按照这些思路即可。

广度优先BFS,深度优先DFS;

LeetCode111_求二叉树最小深度(二叉树问题)的更多相关文章

  1. 【JAVA】【leetcode】【查找二叉树最小深度】

    题目:  minimum-depth-of-binary-tree 要求:Given a binary tree, find its minimum depth.The minimum depth i ...

  2. LinCode 刷题 之二叉树最小深度

    http://www.lintcode.com/zh-cn/problem/minimum-depth-of-binary-tree/  题目描述信息 /** * Definition of Tree ...

  3. [LeetCode] Minimum Depth of Binary Tree 二叉树最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  4. leetcode-111. 二叉树最小深度 · Tree + 递归

    题面 找出二叉树的最小深度(从根节点到某个叶子节点路径上的节点个数最小). 算法 算法参照二叉树的最大深度,这里需要注意的是当某节点的左右孩子都存在时,就返回左右子树的最小深度:如果不都存在,就需要返 ...

  5. 从"按层次输出二叉树"到"求解二叉树深度"的总结

    本文是在学习中的总结,欢迎转载但请注明出处:http://write.blog.csdn.net/postedit/41964669 最近在刷LeetCode上的算法题,发现好多题目的解题思路大体是一 ...

  6. 【easy】111. Minimum Depth of Binary Tree求二叉树的最小深度

    求二叉树的最小深度: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...

  7. LeetCode 111. Minimum Depth of Binary Tree (二叉树最小的深度)

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  8. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  9. LeetCode OJ:Minimum Depth of Binary Tree(二叉树的最小深度)

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

随机推荐

  1. POI解决内存溢出问题

    在POI3.8中SXSSF仅仅支持excel2007格式是对XSSF的一种流的扩展.目的在生成excel时候,需要生成大量的数据的时候,通过刷新的方式将excel内存信息刷新到硬盘的方式,提供写入数据 ...

  2. 洛谷4178 BZOJ1468 Tree题解点分治

    点分治的入门练习. 题目链接 BZOJ的链接(权限题) 关于点分治的思想我就不再重复了,这里重点说一下如何判重. 我们来看上图,假设我们去除了1节点,求出d[2]=1,d[3]=d[4]=2 假设k为 ...

  3. dva与create-react-app的结合使用

    dva与我们的create-react-app创建的两款脚手架是我们写react项目的两款优秀框架,之前一种使用create-react-app这款脚手架进行开发.然后这个框架美中不足的是redux方 ...

  4. c#操作sqlite db3数据库

    首先添加引用 System.Data.SQLite.dll,引用只用添加这个,但SQLite.Interop.dll文件必须也和它同时放在Debug目录下 然后可用: SQLiteConnection ...

  5. js+canvas黑白棋

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. 创建我的flask第一个应用(一)

    本地python版本 (venv) dongge@dongge-PC:/data/webroot$ python --version Python 3.6.5 创建flask命令 $mkdir myp ...

  7. 13-4 jquery操作标签(文本,属性,class,value)

    一 文本操作 $().html() $().text() 文本赋值操作 $().html("") $().text("") 二 属性操作 $().attr(属性 ...

  8. OO第四单元博客作业

    OO第四单元博客作业 BUAA_1706_HugeGun 目录 第四单元作业架构设计 四个单元架构设计及OO方法理解 四个单元测试理解与实践演进 课程收获 一点建议 第四单元作业架构设计 ### 第十 ...

  9. 详解如何在Laravel中增加自定义全局函数

    http://www.php.cn/php-weizijiaocheng-383928.html 如何在Laravel中增加自定义全局函数?在我们的应用里经常会有一些全局都可能会用的函数,我们应该怎么 ...

  10. win7 debug 工具

    x86 处理器中的 CS 与 IP 寄存器介绍与调试: http://blog.sina.com.cn/s/blog_54f82cc2010121yj.html https://www.jianshu ...