您的位置:首页 > 编程语言 > Python开发

leetcode 111 Minimum Depth of Binary Tree

2018-03-06 10:21 267 查看
Java:class Solution {
public int minDepth(TreeNode root) {
if (root == null) return 0;
int l = minDepth(root.left);
int r = minDepth(root.right);
return (l == 0 || r == 0) ? l + r + 1:Math.min(l, r) + 1;
}
}Python:class Solution:
def minDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if root is None:
return 0
l, r = self.minDepth(root.left), self.minDepth(root.right)
return l+r+1 if l==0 or r==0 else min(l, r) + 1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode java python