Minimum Depth of Binary Tree (Leetcode 111)
Problem Link: https://leetcode.com/problems/minimum-depth-of-binary-tree/
class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
if(root is None):
return 0
l=self.minDepth(root.left)
r=self.minDepth(root.right)
if(l==0 and r==0):
return 1
elif(l==0 and r!=0):
return r+1
elif(l!=0 and r==0):
return l+1
else:
return min(r,l)+1
Last updated