Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Info

Given binary tree [3,9,20,null,null,15,7],

3
/ \
9 20
/ \
15 7
return its minimum depth = 2.


Code Block
languagepy
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def minDepth(self, root: TreeNode) -> int:
        if root==None:
            return 0
        
        if root.left==None and root.right==None: return 1
        elif root.left!=None and root.right!=None:
            return min( self.minDepth(root.left), self.minDepth(root.right)) + 1
        elif root.left==None:
            return self.minDepth(root.right) + 1
        else:
            return self.minDepth(root.left) + 1

...