/*
* @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. sql字段合并与分组聚合

    http://blog.csdn.net/cuixianlong/article/details/74024846 1 字段合并 原始数据如下:表名为Employee ID FirstName Las ...

  2. Dynamics CRM RibbonWorkbench工具使用

    这边用的是RibbonWorkbench2016的工具,导入RibbonWorkbench2016解决方案即可.导入成功后在解决方案下面会多出一个快捷键小图标. 一.基本介绍 二.列表页获取选中记录的 ...

  3. bootstrap时时提醒填入数据是否与数据库数据重复

    standardcode: { group: '.col-sm-4',//对应前台input的class占用宽度 validators: { notEmpty: { message: '请输入标准代号 ...

  4. Asterisk 对wav格式的支持

    经过测试wav格式文件仅支持PCM 8000kHz 16bit 单声道,非常蛋疼的一个原因,排查了好久! 关于C#支持的一些格式(Mono 单声道 .Stereo 立体声道) // Standard ...

  5. python3的学习经验

    网上资料非常多,颇有些“乱花渐欲迷人眼”的意味,个人看了不少,拖之前从事前端的福,发现廖雪峰大神的网站里有.学了2天之后发觉获益良多,网址:https://www.liaoxuefeng.com/wi ...

  6. MATLAB安装与注册(血泪总结)

    工具/原料   R2016a_win64.iso(安装文件) Matlab 2016a Win64 Crack.rar(破解文件) 方法/步骤   1 下载R2016a_win64.iso(安装文件) ...

  7. Softmax实现 fashion.mnist 分类

    softmax #!/usr/bin/env python # coding: utf-8 # In[1]: get_ipython().run_line_magic('matplotlib', 'i ...

  8. UVA11294 Wedding

    嘟嘟嘟 大佬们都说这是2-SAT入门题,然而对于刚学2_SAT的本菜鸡来说半天才理解…… 题面:新娘和新郎不能坐在同一侧,妻子和丈夫不能坐在同一侧,有**关系的两个人必须至少一个坐在新娘一侧,问方案. ...

  9. Let’s Encrypt 最近很火的免费SSL 使用教程

    2015年10月份,微博上偶然看到Let's Encrypt 推出了beta版,作为一个曾经被https虐出血的码农来说,这无疑是一个重磅消息.并且在全站Https的大趋势下,Let's Encryp ...

  10. jenkins添加环境变量 ,win 10的 环境变量如下,win7 就是不加也可以运行,不报 “python 不是内部命令 ” 的错误。

    jenkins 添加win 10的 环境变量如下,win7 就是不加也可以运行,不报 “python 不是内部命令 ” 的错误,暂时不知道怎么回事.    jenkins这样添加环境变量 .