Problem:
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.
Solution
# 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 minf(self,a,b):
if a<b:
return a
else:
return b
def minDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if root ==None:
return 0
lmin = self.minDepth(root.left)
rmin = self.minDepth(root.right)
if lmin==0 and rmin==0:
return 1
if lmin==0:
lmin=INT_MAX
if rmin==0:
rmin=INT_MAX
return self.minf(lmin,rmin)+1
版权声明:自由转载-非商用-非衍生-保持署名 froyobin
本文永久链接:
http://froyobin.github.io/home/sample-post/Minimum_Depth_of_Binary_Tree