/*
* @lc app=leetcode.cn id=111 lang=c
*
* [111] 二叉树的最小深度
*
* https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/description/
*
* algorithms
* Easy (37.27%)
* Total Accepted: 12.2K
* Total Submissions: 32.6K
* Testcase Example: '[3,9,20,null,null,15,7]'
*
* 给定一个二叉树,找出其最小深度。
*
* 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
*
* 说明: 叶子节点是指没有子节点的节点。
*
* 示例:
*
* 给定二叉树 [3,9,20,null,null,15,7],
*
* ⁠ 3
* ⁠ / \
* ⁠ 9 20
* ⁠ / \
* ⁠ 15 7
*
* 返回它的最小深度  2.
*
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int min(a,b);
int minDepth(struct 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));
}
int min(a,b){
return a<b?a:b;
}

最小深度和最大深度类似,但是要注意的就是,当左子树为空的时候,只查右子树就可以,右子树为空的时候,只查左子树即可。

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

python:

#
# @lc app=leetcode.cn id=111 lang=python3
#
# [111] 二叉树的最小深度
#
# https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/description/
#
# algorithms
# Easy (37.27%)
# Total Accepted: 12.2K
# Total Submissions: 32.6K
# Testcase Example: '[3,9,20,null,null,15,7]'
#
# 给定一个二叉树,找出其最小深度。
#
# 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
#
# 说明: 叶子节点是指没有子节点的节点。
#
# 示例:
#
# 给定二叉树 [3,9,20,null,null,15,7],
#
# ⁠ 3
# ⁠ / \
# ⁠ 9 20
# ⁠ / \
# ⁠ 15 7
#
# 返回它的最小深度  2.
#
#
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None
class Solution:
def minDepth(self, root):
if root == None:
return 0
elif root.left == None and root.right == None:
return 1
elif root.left == None and root.right != None:
return self.minDepth(root.right)+1
elif root.right ==None and root.left != None:
return self.minDepth(root.left)+1
elif root.left != None and root.right !=None:
return min(self.minDepth(root.left), self.minDepth(root.right))+1

Leecode刷题之旅-C语言/python-111二叉树的最小深度的更多相关文章

  1. Leecode刷题之旅-C语言/python-101对称二叉树

    /* * @lc app=leetcode.cn id=101 lang=c * * [101] 对称二叉树 * * https://leetcode-cn.com/problems/symmetri ...

  2. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  3. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  4. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  5. Leecode刷题之旅-C语言/python-7.整数反转

    /* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...

  6. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  7. Leecode刷题之旅-C语言/python-326 3的幂

    /* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...

  8. Leecode刷题之旅-C语言/python-263丑数

    /* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...

  9. Leecode刷题之旅-C语言/python-383赎金信

    /* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...

随机推荐

  1. Python对数组的基本操作

    # coding=utf-8创建并打印数组'''arr = ["aex", "bfe", "mpilgrim", "zddd&qu ...

  2. Mantis修改“严重性”字段内容

    尝试修改Mantis提交bug时“严重性”字段的内容,将原来的内容修改为A,B,C...等级: 在mantis/config_defaults_inc.php文件中查找$g_severity_enum ...

  3. 【Leetcode】【Easy】String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  4. 【Leetcode】【Easy】Merge Sorted Array

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...

  5. SVN:验证位置时发生错误解决方案

    1. 2. 3.preferencens > svn >svn接口-选择SVNKit(Pure Java)设置后,再引用svn路径后,直接弹出输入用户名和密码就对了. 4.

  6. 7.Zabbix 3.0 web监控

    请查看我的有道云笔记: http://note.youdao.com/noteshare?id=5f6b67f98a802fb831a83d810969c583&sub=B8D5267BDD5 ...

  7. Jerry的CRM Middleware(中间件)文章合集

    我在SAP成都研究院做过的CRM中间件的项目其实并不是很多: 1. 2013年下半年和2014年上半年曾经支持过中联重科和蒙牛的CRM项目相关的中间件问题; 2. 2014年上半年做过一个CRM物料主 ...

  8. darknet53 yolo 下的识别训练

    [目录] 一. 安装Darknet(仅CPU下) 2 1.1在CPU下安装Darknet方式 2 1.2在GPU下安装Darknet方式 4 二. YOLO.V3训练官网数据集(VOC数据集/COCO ...

  9. Ubuntu14.04系统下无法连接Wi-Fi无线网以及安装BCM43142网卡驱动的解决方案

    1.问题描述 博主近日开始学习ROS,首先必装Linux操作系统,选择的是Ubuntu14.04,安装过程略过,直接讲问题.安装完系统之后发现一个重要问题,没法使用Wi-Fi,只能使用有线网络,而且网 ...

  10. ffmpeg视频和声音

    推送视频和声音 ffmpeg -f dshow -i video="screen-capture-recorder" -f dshow -i audio="内装麦克风 ( ...