题目地址
https://leetcode-cn.com/problems/minimum-depth-of-binary-tree
题目描述
代码初步
代码欣赏
# 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 not root:
return 0
minDepth = float('inf')
childList = [root.left,root.right]
# 如果传入的结点既没有左节点,也没有右结点,则返回1
if not any(childList):
return 1
else:
for i in childList:
if i:
minDepth = min(self.minDepth(i),minDepth)
return minDepth+1