Share my python solution with explanation.


  • 4
    W
    class Solution:
        # @param root, a tree node
        # @return an integer
        def minDepth(self, root):
            if not root:
                return 0
            else:
                # if one of the subtree is None, you should return the depth of another subtree.
                # if all of the subtree is not None, you should return the minimum depth of the two subtrees
                if root.left is None:
                    return self.minDepth(root.right) + 1
                elif root.right is None:
                    return self.minDepth(root.left) + 1
                else:
                    return min(self.minDepth(root.left), self.minDepth(root.right)) + 1

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.