【LEETCODE】111-Minimum Depth of Binary Tree
2015-10-31 19:56
471 查看
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.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
# 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: #root为空,返回0 return 0 elif root.left != None and root.right == None: #只有左子树时,不用考虑右子树的深度,否则会返回1,但是无意义的 return self.minDepth(root.left)+1 elif root.left == None and root.right != None: #同理,只有右子树时 return self.minDepth(root.right)+1 else: return min(self.minDepth(root.left),self.minDepth(root.right))+1 #左右子树都有时,返回最小的深度
相关文章推荐
- [LeetCode] [SQL]: 182: Duplicate Emails
- 命名空间namespace
- 移植UBI的5号错误————UBI error: ubi_io_write: error -5 while writing 512 bytes to PEB 0:512, written 0 byte
- #在蓝懿学习iOS的日子#第三个练习日
- 菜鸟学算法之 POJ1011 Sticks
- SQL题
- canvas的两个方法说明
- Linux 网络编程: echo Service
- FS,FT,DFT,DFS和DTFT的关系
- FS,FT,DFT,DFS和DTFT的关系
- iOS--UILabel的使用方法
- Mysql在实际使用中的部署架构
- UML图符总结
- SpringMVC简单地入门程序
- 数独游戏的程序算法
- hdoj--2098--分拆素数和(枚举)
- 区分指针数组的好方法
- Mybatis搭建过程详解
- DL:RBM评估
- 数据库中的几个知识点