题目:

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. 洛谷2375 BZOJ 3670动物园题解

    题目链接 洛谷链接 我们发现题目要我们求的num[i]东西本质上其实是 求有多少以i结尾的非前缀且能与前缀匹配的字符串,而且要求字符串长度小于(i/2) 我们先不考虑字符串长度的限制,看所有以i结尾的 ...

  2. MaxCompute Mars开发指南

    Mars 算法实践 人脸识别 Mars 是一个基于矩阵的统一分布式计算框架,而且 Mars 已经在 GitHub 中开源.当你看完 Mars 的介绍可能会问它能做什么,这几乎取决于你想做什么,因为 M ...

  3. 详解Python中内置的NotImplemented类型的用法

    它是什么? ? 1 2 >>> type(NotImplemented) <type 'NotImplementedType'> NotImplemented 是Pyth ...

  4. 模板—插头dp(Ural 1519 Formula 1)

    括号表示法: 据说比下一个要快而且灵活. #include<iostream> #include<cstring> #include<cstdio> #define ...

  5. HDU3844 Mining Your Own Business

    HDU3844 Mining Your Own Business 问题描述John Digger是一个大型illudium phosdex矿的所有者.该矿山由一系列隧道组成,这些隧道在各个大型交叉口相 ...

  6. oracle SELECT子句中避免使用 ‘ * ‘

    当你想在SELECT子句中列出所有的COLUMN时,使用动态SQL列引用 ‘*’ 是一个方便的方法. 不幸的是,这是一个非常低效的方法. 实际上,ORACLE在解析的过程中, 会将’*’ 依次转换成所 ...

  7. 从 Apache ORC 到 Apache Calcite | 2019大数据技术公开课第一季《技术人生专访》

    摘要: 什么是Apache ORC开源项目?主流的开源列存格式ORC和Parquet有何区别?MaxCompute为什么选择ORC? 如何一步步成为committer和加入PMC的?在阿里和Uber总 ...

  8. hdu 3805 Triangle Conjecture

    Problem - 3805 题意是给出边的长度的,求出边长相等的三角形,输出任意一种答案.边长是1~n的数,每个只能用一次. 其实比较容易可以看出,无论我们怎么操作,只要保持边长总和都是相邻整数就是 ...

  9. Python的内置方法,abs,all,any,basestring,bin,bool,bytearray,callable,chr,cmp,complex,divmod

    Python的内置方法 abs(X):返回一个数的绝对值,X可以是一个整数,长整型,或者浮点数,如果X是一个复数,此方法返回此复数的绝对值(此复数与它的共轭复数的乘积的平方根) >>> ...

  10. BZOJ 4034"树上操作"(DFS序+线段树)

    传送门 •题意 有一棵点数为 N 的树,以点 1 为根,且树点有边权. 然后有 M 个操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中所有点的 ...