leetcode:Minimum Depth of Binary Tree【Python版】
1、类中递归调用添加self;
2、root为None,返回0
3、root不为None,root左右孩子为None,返回1
4、返回l和r最小深度,l和r初始为极大值;
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param root, a tree node
# @return an integer
def minDepth(self, root):
if root == None:
return 0
if root.left==None and root.right==None:
return 1
l,r = 9999,9999
if root.left!=None:
l = self.minDepth(root.left)
if root.right!=None:
r = self.minDepth(root.right)
if l<r:
return 1+l
return 1+r
leetcode:Minimum Depth of Binary Tree【Python版】的更多相关文章
- leetcode Minimum Depth of Binary Tree python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- 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 ...
- 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 ...
- [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 ...
- LeetCode - Minimum Depth of Binary Tree
题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...
- [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 ...
- leetcode Maximum Depth of Binary Tree python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- LeetCode Minimum Depth of Binary Tree 找最小深度(返回最小深度)
题意:找到离根结点最近的叶子结点的那一层(设同一层上的结点与根结点的距离相等),返回它所在的层数. 方法有: 1.递归深度搜索 2.层次搜索 方法一:递归(无优化) /** * Definition ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
随机推荐
- MSSQL 一坑 SQL Management Studio 管理工具的快捷方式被删掉了
如果确定已经安装的情况下,到这里去找下吧(我这里用的是sql 2008) C:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\Co ...
- English trip -- VC(情景课) 8 A Get ready
Words cashier # 收银员 a cashier counts money 收钱 custodian # 清洁工 a custodian cl ...
- MySQL Replication 线程(理解详细过程)
Replication 线程 Mysql 的Replication 是一个异步的复制过程,从一个Mysql instace(我们称之为Master)复制到另一个Mysql instance(我们称之S ...
- python-day71--django多表操作
表关系: 1 一对多 2 多对多 3 一对一 添加记录: 一对多:书与出版社 #创建一对多: publish=models.ForeignKey("Publish") 注意:pub ...
- hpu积分赛(回溯法)
问题 : 不开心的小明① 时间限制: 1 Sec 内存限制: 128 MB 提交: 2 解决: 1 题目描述 一天, 小明很不开心,先是向女神表白被拒, 数学又考了0分, 回家的路上又丢了钥匙, 他非 ...
- SHA1加密算法 java
//下面四个import放在类名前面 包名后面 //import java.io.UnsupportedEncodingException; //import java.security.Messag ...
- C语言 string::size_type类型
string::size_type类型 从逻辑上来讲,size()成员函数似乎应该返回整型数值,或如2.2节“建议”中所述的无符号整数.但事实上,size操作返回的是string::size_type ...
- L1-018 大笨钟
微博上有个自称“大笨钟V”的家伙,每天敲钟催促码农们爱惜身体早点睡觉.不过由于笨钟自己作息也不是很规律,所以敲钟并不定时.一般敲钟的点数是根据敲钟时间而定的,如果正好在某个整点敲,那么“当”数就等于那 ...
- ReentrantLock的原理解析
重入锁(ReentrantLock)是一种可重入无阻塞的同步机制.性能同synchronized接近(老版本jdk中性能很差). 下面重点看下常用的lock()和unlock()方法的实现原理. lo ...
- SWIFT中正则表达式验证邮箱
在playground内写入以下代码,正则关键字跟其它语言的没什么区别 class Regex { let internalExpression:NSRegularExpression let pat ...