题目描述:

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. 洛谷——P1002 过河卒||codevs——T1010 过河卒

    https://www.luogu.org/problem/show?pid=1002#sub||http://codevs.cn/problem/1010/ 题目描述 棋盘上A点有一个过河卒,需要走 ...

  2. MyBatis在注解上使用动态SQL(@select使用if)

    1.用script标签包围,然后像xml语法一样书写 @Select({"<script>", "SELECT * FROM tbl_order", ...

  3. Spring Boot实例Hello World Demo

    Spring Boot要求Maven的版本达到3.2或以上. 实例: POM: <project xmlns="http://maven.apache.org/POM/4.0.0&qu ...

  4. TensorFlow 入门之手写识别(MNIST) softmax算法 二

    TensorFlow 入门之手写识别(MNIST) softmax算法 二 MNIST Fly softmax回归 softmax回归算法 TensorFlow实现softmax softmax回归算 ...

  5. 使用Html5和Js进行拖动

    function init() {             var source = document.getElementById("dragme");             ...

  6. 转:APP测试总结

  7. 数据结构之---C语言实现共享栈

    所谓共享栈是两个栈在一个顺序的存储空间中.两个栈的栈底各自是存储空间的首尾地址. 如图我们能够将两个栈构造成一个: 如图: 从这里也就能够分析出来,栈1为空时,就是top1等于-1时.而当top2等于 ...

  8. QUERY_REWRITE_INTEGRITY

    QUERY_REWRITE_INTEGRITY Property Description Parameter type String Syntax QUERY_REWRITE_INTEGRITY = ...

  9. Spark MLlib介绍

    Spark MLlib介绍 Spark之所以在机器学习方面具有得天独厚的优势,有以下几点原因: (1)机器学习算法一般都有很多个步骤迭代计算的过程,机器学习的计算需要在多次迭代后获得足够小的误差或者足 ...

  10. hdoj--2015--偶数求和(水题)

    偶数求和 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...