[LeetCode]题解(python):111 Minimum Depth of Binary Tree
题目来源
https://leetcode.com/problems/minimum-depth-of-binary-tree/
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.
题意分析
Input: a binary tree
Output: leaf node 的最小深度
Conditions: 注意一定要是leaf节点,求leaf节点的最小深度
题目思路
递归,注意与求最大深度不同
AC代码(Python)
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def minDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if root == None:
return 0
if root.left == None and root.right != None:
return self.minDepth(root.right) + 1
if root.left != None and root.right == None:
return self.minDepth(root.left) + 1
return min(self.minDepth(root.left), self.minDepth(root.right)) + 1
[LeetCode]题解(python):111 Minimum Depth of Binary Tree的更多相关文章
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- 【LeetCode】111. Minimum Depth of Binary Tree (2 solutions)
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- [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 ...
- Leetcode 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- 【LeetCode】111. Minimum Depth of Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...
- 【leetcode❤python】 111. Minimum Depth of Binary Tree
#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):# def __init ...
- 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 ...
- 【一天一道LeetCode】#111. Minimum Depth of Binary Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- leetcode 111 Minimum Depth of Binary Tree(DFS)
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
随机推荐
- Bitset[HDU2051]
Bitset Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- extjs 选项卡
yufenghou extjs 选项卡 <%@ page language="java" import="java.util.*" pageEncodin ...
- HDU 2853 & 剩余系+KM模板
题意: 给你一张二分图,给一个原匹配,求原匹配改动最少的边数使其边权和最大. SOL: 我觉得我的智商还是去搞搞文化课吧..这种题给我独立做我大概只能在暴力优化上下功夫.. 这题的处理方法让我想到了剩 ...
- Remove Duplicates from Sorted List II leetcode java
题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- 定时任务之Spring与Quartz的整合(有修改)
转摘:http://www.javaweb1024.com/java/JavaWebzhongji/2015/04/13/548.html 在Spring中使用Quartz有两种方式实现:第一种是任务 ...
- SVN提交提示:working copy is not up-to-date解决方法
解决方法: 在相应文件上,单击选择team,然后选择先更新,然后再提交.这样就好了.
- mysql root密码破解
破解root密码:方法一:1./etc/my.cnf在[mysqld]段中加入skip-grant-table2.重启mysql3.直接mysql登录3.通过修改权限表方式修改mysql密码(upda ...
- LiveUpdate Adminstrator配置手册
第一种模式: LUA 从Symantec官网LiveUpdate服务器下载更新. .登陆LUA控制台 图1 .添加Symantec Endpoint Protecton v11.0 图2 3. 查看源 ...
- Eclipse学习记录
设置背景色:http://jingyan.baidu.com/article/2a138328b5d9ea074a134fc7.html 项目文件说明:http://www.cnblogs.com/p ...
- UIButton 长按点击 背景改变效果
1.添加按钮的点击事件和按下事件 [btn setImage:[UIImage imageNamed:@"NorMal.png"] forState:UIControlStateN ...