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. WinForm webbrowser控件的使用

    webbrowser是一个比较实用的工具,主要用于在winform窗体中嵌入浏览器,达到winform与webform互操作的目的. 先上一个demo,看一下能实现什么效果. private void ...

  2. weui-switch开关控件,表单提交后如何取值

    最近在学习weui这个框架,做了一些小的试验,发现weui-switch控件直接提交不能获取到表单信息,在segmentfault上发现也有人提了这个问题,有人说可以设置一个隐含标签来捕获开关的状态, ...

  3. node session会话

    会话机制: 浏览器-->服务器 浏览器<--服务器 (产生一个会话id,保存在cookie中) 浏览器-->服务器(服务器根据会话id关联到相应数据信息体data) var expr ...

  4. 【Python让生活更美好01】os与shutil模块的常用方法总结

    Python作为一种解释型的高级语言,脚本语言,又被称作“胶水语言”,就是因为其灵活的语法和其依靠浩如烟海的第三方包实现的丰富多彩的功能,而os和shutil就是这样一种功能强大的模块,可以非常快捷地 ...

  5. 为什么我要放弃javaScript数据结构与算法(第一章)—— JavaScript简介

    数据结构与算法一直是我算比较薄弱的地方,希望通过阅读<javaScript数据结构与算法>可以有所改变,我相信接下来的记录不单单对于我自己有帮助,也可以帮助到一些这方面的小白,接下来让我们 ...

  6. PHP HashTable总结

    本篇文章主要是对 PHP HashTable 总结,下面的参考链接是很好的学习资料. 总结 HashTable 又叫做散列表,是一种用于以常数平均时间执行插入.删除和查找的技术.不能有效的支持元素之间 ...

  7. 2457: [BeiJing2011]双端队列

    2457: [BeiJing2011]双端队列 链接 很奇妙的转化. 题目要求最后的所有序列也是有序的,所以可以求出最后的序列(即排序后的序列),然后分成许多份,要求每一份都是一个双端序列,求最少分成 ...

  8. 读取Excel错误,未在本地计算机上注册 oledb.4.0

          以前写的一个读取Excel的程序,现在在另外一台机器上运行,竟然报错说"未在本地计算机上注册 oledb.4.0"       最后才知道,原来是因为现在运行的那台电脑 ...

  9. Smart Framework:轻量级 Java Web 框架

    Smart Framework:轻量级 Java Web 框架 收藏 黄勇   工作闲暇之余,我开发了一款轻量级 Java Web 框架 —— Smart Framework. 开发该框架是为了: 加 ...

  10. Scala学习笔记(一):环境搭建

    计算机领域的编程语言种类繁多,如C.C++.Java.C#等,我们知道的一般都是较为流行的编程语言,然有更多的是没听说过的,于是也就说不上关注或者使用了 一次在网上查资料时,无意间看到了“函数式编程” ...