题目描述:

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. MVVM设计模式基础知识--ICommand接口

    命令是 Windows Presentation Foundation (WPF) 中的输入机制,它提供的输入处理比设备输入具有更高的语义级别. 命令有若干用途: 第一个用途是将语义以及调用命令的对象 ...

  2. JS计算两时间的天数

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> </head> <body>& ...

  3. 虚拟机window7与主机之间文件复制设置

    一.需要安装VMware Tools 选中虚拟机>虚拟机>安装VMware Tools 一直点击下一步直至完成 二.设置文件共享 选定实体机需要共享给虚拟机的文件夹,并为该共享起一个名称. ...

  4. MongoDB 索引的使用, 管理 和优化

    MongoDB 索引的使用, 管理 和优化 2014-03-25 17:12 6479人阅读 评论(0) 收藏 举报  分类: MongoDB(9)  [使用explain和hint] 前面讲高级查询 ...

  5. nyoj--239--月老的难题(最小点覆盖)

    月老的难题 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 月老准备给n个女孩与n个男孩牵红线,成就一对对美好的姻缘. 现在,由于一些原因,部分男孩与女孩可能结成幸福的一 ...

  6. Solr.NET快速入门(七)【覆盖默认映射器,NHibernate集成】

    覆盖默认映射器 默认情况下,SolrNet使用属性映射Solr字段. 但是,您可能需要使用另一个映射程序. 替换默认映射器取决于您如何设置库: 内置容器 如果使用默认的内置容器,可以在调用Startu ...

  7. 6.Renderer Window

    渲染是实时的,所见即所得.同时还可以输出一些统计信息. Pixel Snoop:获取颜色值,同时把该值复制到剪贴板,主要用户是获取颜色值 Wireframe:开启后可以查看3D节点图形骨架 Stati ...

  8. python课程设计笔记(四)整数、浮点数与字符串 time库

    整数类型(范围无限制) 十进制1 -1 二进制0b1 -0b1 八进制0o1 -0o1 十六进制0x1 -0x1 浮点类型(范围有限制但可忽略) 运算存在不确定尾数 :0.1+0.2!=0.3 原因: ...

  9. web拼图错误分析

    老师要求用web制作一个拼图游戏. 发现的问题:点击随机生成拼图的按钮后,打乱的图片会出现无法还原的情况. 发现过程:每次生成一个拼图后会测试它怎么拼回去,结果发现有时候拼不回去. 数学原理:如果两个 ...

  10. <a>和<table>标签的应用

    今天介绍一下html中最重要的标签 标签分为 1.一般标签 如<img> <b></b>等 2.功能标签 如<a></a> 3.实体 如&a ...