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
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 111 Minimum Depth of Binary Tree(DFS)
- leetcode——111——Minimum Depth of Binary Tree
- [leetcode 111] Minimum Depth of Binary Tree
- 【leetcode c++】111 Minimum Depth of Binary Tree
- LeetCode111 Minimum Depth of Binary Tree
- LeetCode题解-111-Minimum Depth of Binary Tree
- [leetcode-111]Minimum Depth of Binary Tree(C语言)
- Leetcode #111 Minimum Depth of Binary Tree
- LeetCode#111 Minimum Depth of Binary Tree
- LeetCode 111:Minimum Depth of Binary Tree
- leetcode 111 Minimum Depth of Binary Tree
- [LeetCode 111] Minimum Depth of Binary Tree
- 【LeetCode-面试算法经典-Java实现】【111-Minimum Depth of Binary Tree(二叉树的最小深度)】
- leetcode_c++:树: Minimum Depth of Binary Tree(111)
- LeetCode 111: Minimum Depth of Binary Tree
- LeetCode 111 Minimum Depth of Binary Tree(DFS)
- leetcode_111题——Minimum Depth of Binary Tree(二叉树,队列)
- LeetCode111—Minimum Depth of Binary Tree
- LeetCode 111:Minimum Depth of Binary Tree
- [leetcode111].Minimum Depth of Binary Tree