Leecode刷题之旅-C语言/python-111二叉树的最小深度
/*
* @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二叉树的最小深度的更多相关文章
- Leecode刷题之旅-C语言/python-101对称二叉树
/* * @lc app=leetcode.cn id=101 lang=c * * [101] 对称二叉树 * * https://leetcode-cn.com/problems/symmetri ...
- Leecode刷题之旅-C语言/python-1.两数之和
开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- Leecode刷题之旅-C语言/python-28.实现strstr()
/* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...
- Leecode刷题之旅-C语言/python-7.整数反转
/* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...
- Leecode刷题之旅-C语言/python-434 字符串中的单词数
/* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...
- Leecode刷题之旅-C语言/python-326 3的幂
/* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...
- Leecode刷题之旅-C语言/python-263丑数
/* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...
- Leecode刷题之旅-C语言/python-383赎金信
/* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...
随机推荐
- 【邀请函】小投入 大产出—微软智能云(Azure)之CDN 专题
会议时间 2016 年 11 月 24 日 14:00-16:00 会议介绍 尊敬的客户: 您是否还在为如何提高网站的响应速度以及用户增长造成的源站压力烦恼?是否还在担心源站 IP 暴露存在安全隐患? ...
- jmeter中CSV Data Set Config各项说明
Config the CSV Data Source: 1)Filename:csv文件的名称(包括绝对路径,当csv文件在bin目录下时,只需给出文件名即可) 2)File encoding:csv ...
- 一点一点学写Makefile(2)-自动搜所当前目录下的所有源文件
上个博客我们使用的是笨方法添加源文件,本次我要实现的是遍历文件夹来获得所有的cpp文件 //makefile CROSS = CC = $(CROSS)gcc CXX = $(CROSS)g++ DE ...
- 同时开左右两个SAPGUI编辑器显示同一段ABAP代码
很多文本编辑器都支持同时开左右两个窗口显示同一段代码,使用场景可能是比较同一段代码的不同版本差异,或者是ABAP里,同一段代码在Netweaver不同版本里的实现差异,比如版本为SP1的系统A和版本为 ...
- 另一种方式实现事务码SE16里的结果集修改
注: 这种方法不同于网上流传的在调试器里修改fcode的那种解决方案. 使用场景:我们需要直接在开发系统的事务码SE16里修改某些结果集的值,但是在SE16的工具栏里看不见修改按钮: 解决方案 使用/ ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 【优先队列维护最小堆+贪心】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6438 Buy and Resell Time Limit: 2000/1000 MS (Java/O ...
- Hive UDF 用户自定义函数 编程及使用
首先创建工程编写UDF 代码,示例如下: 1. 新建Maven项目 udf 本机Hadoop版本为2.7.7, Hive版本为1.2.2,所以选择对应版本的jar ,其它版本也不影响编译. 2. po ...
- [转]这13个开源GIS软件,你了解几个?
这些开源GIS软件,你了解几个?本文内容部分来源于一份罗列了关于GIS软件应用的文章,笔者将其编译整合. 地理信息系统(Geographic Information System,GIS)软件依赖于覆 ...
- jsp的4个作用域区别( pageScope、requestScope、sessionScope、applicationScope)
简单描述 page里的变量没法从index.jsp传递到test.jsp.只要页面跳转了,它们就不见了. request里的变量可以跨越forward前后的两页.但是只要刷新页面,它们就重新计算了. ...
- 原生ajax接收json字符串(简单介绍)
什么是json? JSON的全称是 Javascript Object Notation(javascript对象表示法),是基于javascript对象字面量,如果单从眼睛看,JSON里的数据是被保 ...