题目描述:

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. SEAndroid安全机制框架分析

    我们知道,Android系统基于Linux实现. 针对传统Linux系统,NSA开发了一套安全机制SELinux,用来加强安全性. 然而.因为Android系统有着独特的用户空间执行时.因此SELin ...

  2. leetcode解题文件夹

    点击打开链接点击打开链接点击打开链接參考文献:http://blog.csdn.net/lanxu_yy/article/details/17848219 只是本文准备用超链接的方式连接到对应解答页面 ...

  3. fzu 1075 分解素因子

    代码: #include<cstdio> #include<cstring> #include<iostream> using namespace std; int ...

  4. POJ1742 Coins 背包

    题目大意:给出一些钱币的价值和对应的数目,求在一定价值限定下这些钱币能凑成的价值数. 本题用多重背包直接拆分或二进制拆分法都太慢.说起处理一组物品,完全背包可算是比较效率高的,但是本题中物体的数目是有 ...

  5. JQuery常用的api[最好是系统地学习一下《锋利的JQuery》]

    text http://api.jquery.com/text/ Get the combined text contents of each element in the set of matche ...

  6. Linux - 目录结构与查看,复制,删除,剪切指令

    Linux当中,一切皆文件. Linux目录结构 / 根分区,只有root用户对此目录拥有写权限. /etc 配置文件 /boot 启动文件 /var 可增长的目录 .日志,文件等. /root 管理 ...

  7. xocde8打印出:Presenting view controllers on detached view controllers is discouraged

    原因: 是某个viewController的生命周期控制出现了错误,所以尽量避免一个controller的view去addsubview另一个controller的view,这样会破坏层级关系,导致第 ...

  8. Spring《八》AOP/代理类定义

    Spring通知 Interception Around通知 MethodInterceptor类(方法执行前后通知) Before通知 MethodBeforeAdvice类(方法执行前通知) Af ...

  9. C# How to convert MessageBodyStream to MemoryStream?

    通过WCF服务从数据库取文档数据时,返回的是Stream对象,在DevExpress的PDFViewer显示时,用PDFViewer.LoadDocunent(Stream stream);方法时,报 ...

  10. 第六课: - GroupBy函数

    第 6 课   让我们看一看 groupby 函数. In [1]: # Import libraries import pandas as pd import sys In [2]: print(' ...