Skip to main content

111. Minimum Depth of Binary Tree

class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:


def dfs(node):
if not node:
return 0

left = dfs(node.left)
right = dfs(node.right)

if not left and not right:
return 1

if left and right:
return min(left, right) + 1

if left:
return left + 1

return right + 1


return dfs(root)
class Solution(object):
def minDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root:
return 0

return self.iterative_level([root], 1)


def iterative_level(self, nodes, i):
next_level = []
for node in nodes:
if not node.left and not node.right:
return i
if node.left:
next_level.append(node.left)
if node.right:
next_level.append(node.right)

return self.iterative_level(next_level, i+1)