题目描述:

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.

特殊输入:

只有两个节点时,树的深度是2。但是如果只考虑左子树或右子树为空。结果会返回1.

两个错误的代码:(不应该以子树空判断结束)

实现1:该代码是错误的
class Solution {
public:
int run(TreeNode *root) {
if(root==nullptr)
return 0; queue<TreeNode *> currQueue;
currQueue.push(root);
int count = 0;
while(!currQueue.empty()){
count++;
for(int i=0;i<currQueue.size();i++){ //遍历一层
TreeNode * pNode = currQueue.front();
currQueue.pop();
if(pNode->left)
currQueue.push(pNode->left);
else
return count;
if(pNode->right)
currQueue.push(pNode->right);
else
return count; }
}
return count;
}
}
//实现2:  该代码是错误的    
int getMinDepth(TreeNode *root){
if(root==nullptr)
return 0;
return min(getMinDepth(root->left),getMinDepth(root->right))+1;
}

解题思路:

1)一直访问到叶子节点,层次遍历遇到的最早的叶子节点,当前层就是树的最小深度。(使用队列,非递归)

   1
/ \
2 3
/
4
错误的方法是:当子树为空,返回当前层(上面的例子可以)
但是下面的例子会返回1。
1
/
2
class Solution {
public:
int run(TreeNode *root) {
if(root==nullptr)
return 0; queue<TreeNode *> currQueue;
currQueue.push(root);
int count = 0;
while(!currQueue.empty()){
count++;
int size = currQueue.size();
for(int i=0;i<size;i++){ //遍历一层
TreeNode * pNode = currQueue.front();
currQueue.pop();
if(pNode->left==nullptr && pNode->right==nullptr) //遇到的第一个叶子节点,返回当前层
return count;
if(pNode->left)
currQueue.push(pNode->left);
if(pNode->right)
currQueue.push(pNode->right);
}
}
return count;
} };

2)思路:递归

若为空树返回0;
若左子树为空,则返回右子树的最小深度+1;(加1是因为要加上根这一层,下同)
若右子树为空,则返回左子树的最小深度+1;
若左右子树均不为空,则取左、右子树最小深度的较小值,+1;
//实现1
class Solution {
public:
int run(TreeNode *root)
{
if(root == nullptr) return 0;
if(root->left == nullptr) // 若左子树为空,则返回右子树的最小深度+1
{
return run(root->right)+1;
}
if(root->right == nullptr) // 若右子树为空,则返回左子树的最小深度+1
{
return run(root->left)+1;
}
// 左右子树都不为空时,取较小值
int leftDepth = run(root->left);
int rightDepth = run(root->right);
return (leftDepth<rightDepth)?(leftDepth+1):(rightDepth+1);
}
};
//实现2
class Solution {
public:
int run(TreeNode *root) {
if(!root)
return 0;
int l = run(root->left);
int r = run(root->right);
if(l==0 || r==0)
return 1+l+r;
return 1+min(l,r);
}
};

3)深度遍历要遍历所有节点,因此选广度优先遍历更好。不再给出深度遍历的代码  

总结:

二叉树操作主要还是利用尾递归或者循环遍历这两种思路,进而涉及DFS(主要利用递归或者实现)或者BFS(主要利用队列实现)。  

N1-1 - 树 - Minimum Depth of Binary Tree的更多相关文章

  1. [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  2. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

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

  3. LeetCode: Minimum Depth of Binary Tree 解题报告

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

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

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

  5. 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...

  6. 【LeetCode练习题】Minimum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

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

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

  8. LeetCode My Solution: Minimum Depth of Binary Tree

    Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...

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

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

随机推荐

  1. 线上MySQL慢查询现象案例--Impossible WHERE noticed after reading const tables

    前言:2012年的笔记整理而得,发布个人博客,做备忘录使用. 背景:线上慢查询日志监控,得到如下的语句:       发现:select doc_text from t_wiki_doc_text w ...

  2. Java-基本输入输出

    Scanner sc = new Scanner(System.in); System.out.println("Please input the path:"); String ...

  3. 使用URL在线语音合成

    近期一直在做手机的项目,用到了语音合成与识别的功能.就找了几个网址做了分析,这里只实现了内容的合成.并不包括语音识别. 首先看一下谷歌的语音合成地址: http://translate.google. ...

  4. 《随笔》pyqt 获取 TreeWidget 选中项的内容

    感谢朋友支持本博客,欢迎共同探讨交流.因为能力和时间有限,错误之处在所难免.欢迎指正! 假设转载,请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...

  5. QQ好友列表数据模型封装

    QQ好友中的信息较多.假设我们单独从plist 中直接取出数据 是能够解决这个问题 可是相当复杂.以为列表中分组 .每组中还有不同信息 大致模型是 数组套数组  数组套字典 所以我们要封装数据模型 / ...

  6. 【cl】maven新建项目

    http://blog.csdn.net/sushengmiyan/article/details/40142771 使用maven创建一个helloworld 在本地硬盘创建一个文件夹作为maven ...

  7. 浅析hybrid模式下地支付宝钱包和微信

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2VuY2hhbzEyNg==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  8. SQL Server数据库 bcp导出备份文件应用

    /**  * 授权  */ EXEC sp_configure 'show advanced options',1; go reconfigure; go exec sp_configure 'xp_ ...

  9. linux for LVM 创建笔记

    LVM: 1.创建pv(物理卷) [root@localhost dev]# pvcreate /dev/sdd /dev/sde /dev/sdf Writing physical volume d ...

  10. php执行运算符

    php执行运算符 简介 php 支持一个执行运算符:反引号(``).反引号(``)位于键盘Tab键左上方.php 将尝试将反引号中的内容作为外壳命令来执行,并将其输出信息返回(例如,可以赋给一个变量而 ...