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.

方法一:

层次遍历。主体的思想不变,程序的主体结构也不变,具体遍历过程参照二叉树的层次遍历。关键在于终止条件,在某一层中,节点遍历的顺序是从左往右的,若是最短路径出现在该层中,则其中一定有某一节点的左、右孩子均不存在,即为叶节点。返回level+1是因为最小的深度要算该节点所在的层。

class Solution {
public:
int run(TreeNode *root)
{
if(root==NULL) return ;
int level=;
queue<TreeNode *> Q;
Q.push(root);
while( !Q.empty())
{
int levNum=;
int count=Q.size(); while(levNum<count)
{
TreeNode *temp=Q.front();
Q.pop();
if(temp->left==NULL&&temp->right==NULL)
return level+;
if(temp->left)
Q.push(temp->left);
if(temp->right)
Q.push(temp->right); levNum++;
}
level++;
}
return level;
}
};

方法二:

思想还是层次遍历,写法不一样。make_pair中第一元素为节点,第二个元素为改节点所在的层数。即让每个进入队列中的节点都携带所在层数信息。非本人原创

class Solution {
public:
int run(TreeNode *root)
{
queue<pair<TreeNode *,int>> Q;
if(root==NULL) return ;
Q.push(make_pair(root,)); while(!Q.empty())
{
pair<TreeNode *,int> cur=Q.front();
Q.pop();
if(cur.first->left==NULL&&cur.first->right==NULL)
return cur.second; if(cur.first->left)
Q.push(make_pair(cur.first->left,cur.second+));
if(cur.first->right)
Q.push(make_pair(cur.first->right,cur.second+));
}
return ;
}
};

方法三:

递归算法,参见Grandyang

class Solution {
public:
int minDepth(TreeNode *root) {
if (root == NULL) return ;
if (root->left == NULL && root->right == NULL) return ; if (root->left == NULL) return minDepth(root->right) + ;
else if (root->right == NULL) return minDepth(root->left) + ;
else return + min(minDepth(root->left), minDepth(root->right));
} };

递归的另一种写法:@牛客网友

class Solution {
public:
int run(TreeNode *root)
{
if(root==NULL) return ; int lChild=run(root->left);
int rChild=run(root->right); if(lChild==||rChild==)
return +lChild+rChild;
return +min(lChild+rChild);
}
};

[Leetcode] The minimum depth of binary tree二叉树的最小深度的更多相关文章

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

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

  2. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  3. [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 ...

  4. [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 ...

  5. LeetCode:111_Minimum Depth of Binary Tree | 二叉树的最小深度 | Easy

    要求:此题正好和Maximum Depth of Binary Tree一题是相反的,即寻找二叉树的最小的深度值:从根节点到最近的叶子节点的距离. 结题思路:和找最大距离不同之处在于:找最小距离要注意 ...

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

    给定一个二叉树,找出其最小深度.最小深度是从根节点到最近叶节点的最短路径的节点数量.详见:https://leetcode.com/problems/minimum-depth-of-binary-t ...

  7. Leetcode 111 Minimum Depth of Binary Tree 二叉树

    找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...

  8. LeetCode OJ Minimum Depth of Binary Tree 递归求解

        题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...

  9. [LeetCode] 104. Maximum Depth of Binary Tree 二叉树的最大深度

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

随机推荐

  1. 中间件kafka

    * kafka----一个发布订阅消息系统,中间件:一个分布式.分区.可重复的日志服务kafka需要了解基础几层结构,生产者订阅者等使用方法,和在高并发.一致性场景使用.(凡事面试问一致性.高并发都脱 ...

  2. Spring笔记2

    Bean生命周期 1 实例化 2 注入属性 3 BeanNameAware 4 BeanFactoryAware 5 ApplicationContextAware 6 BeanPostProcess ...

  3. MongoDB学习(1)--安装,基本curd操作

    知识点: 1-MongoDB 安装,启动和卸载 2-基本概念 3-基本的增删改查操作(CURD) 来回顾总结一把学习的mongodb,如果有javascript基础,学习"芒果DB" ...

  4. Python3爬虫(三)请求库的使用之urllib

    Infi-chu: http://www.cnblogs.com/Infi-chu/ 一.urllib库: 1. 是Python内置的HTTP请求库 2. 在Python2中,由urllib和urll ...

  5. COURSES POJ1469(模板)

    Description Consider a group of N students and P courses. Each student visits zero, one or more than ...

  6. JSOI2018 R1 & 九省联考2018 滚粗记

    在NOIP与PKUWC相继滚粗后,rp守恒定律似乎终于开始起作用了…… (尽管Day2依然滚粗?) Day1: 本着前40min不写代码的准则,先把三道题大致过了一遍,似乎都比较喜闻乐见? T1:对抗 ...

  7. Java 中编码与摘要算法

    URL 编码与解码 String s = "你好,世界!"; // URL 编码 String urlEncodedString = URLEncoder.encode(s, &q ...

  8. HTML5 + JS 调取摄像头拍照下载

    <video id="video" width="640" height="480" autoplay></video&g ...

  9. android开发过程中项目中遇到的坑----布点问题

    我们在红点push 的到达和点击的地方,都加了布点.后来功能上了线,发现,每天的点击都比到达高! 这肯定不科学. 赶紧查问题,打开程序,发红点,关闭程序,布点上传.没问题.数据部门可以收到红点啊! 从 ...

  10. Tapestry 权威讲解-备份

    http://blog.csdn.net/mindhawk/article/details/5021371#introduction