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. I/O流、文件操作

    1)操作文件 Path和Files是在JavaSE7中新添加进来的类,它们封装了在用户机器上处理文件系统所需的所有功能.Path表示的一个目录名序列,其后还可以跟着一个文件名.路径中的第一个参数可以是 ...

  2. hadoop生态搭建(3节点)-07.hive配置

    # http://archive.apache.org/dist/hive/hive-2.1.1/ # ================================================ ...

  3. 函数:引用file类对象及io类对象作为参数打印文本及显示文本

    #include <iostream> #include <fstream> #include <cstdlib> using namespace std; voi ...

  4. Python3 os模块&sys模块&hashlib模块

    ''' os模块 非常重要的模块 ''' import os # print(os.getcwd()) # 获取当前工作目录 # os.chdir(r'路径名') # 改变当前工作目录 # print ...

  5. Hbase数据IO

    场景及方案分析 场景1:logs --> HBase logs -> flume -> hfile -> import -> HBase (实时) csv导入HBase ...

  6. Android面试收集录 Android组件

    1.请说出Android SDK支持哪些方式显示富文本信息? 使用TextView组件可以显示富文本信息,如果要实现图文混排,需实现ImageGetter接口 使用WebView组件显示HTML页面 ...

  7. Java 基础------16进制转2进制

    我们知道,数字8用二进制表示为:1000 用16进制表示为:8 那么我给你一个16进制的数字,0x7f,他的二进制是什么呢? 一个16进制的位数,用4位表示.比如,0x 7 f 其中: 7用4位二进制 ...

  8. windows 10 下的linux子系统用法 -- tmux分屏工具用法

    1 激活linux子系统的方法见百度: 2 打开powershell,输入bash启动子系统终端:输入exit退出: 3 输入tmux attach连接会话:ctrl-b+d 返回终端:ctrl-b+ ...

  9. PostgreSQL字段名和表名大小写的问题

    创建表的时候,表名和字段名必须全小写,然后查询的时候不管全大写或全小写,或是Camel模式都不会报错.只要名称中有大写字母,或者全大写,查询时就必须保证大小写正确并用双引号包起来,否则就会报“XXX不 ...

  10. Android应用AsyncTask处理机制详解及源码分析

    1 背景 Android异步处理机制一直都是Android的一个核心,也是应用工程师面试的一个知识点.前面我们分析了Handler异步机制原理(不了解的可以阅读我的<Android异步消息处理机 ...