111. Minimum Depth of Binary Tree

Intuition

This problem could be solved recursivly by DFS.

Solution

class Solution:
    def minDepth(self, root: TreeNode) -> int:
        if root is None:
            return 0
        if root.left == root.right == None:
            return 1

        if root.right is None:
            return self.minDepth(root.left) + 1
        if root.left is None:
            return self.minDepth(root.right) + 1
        return min(self.minDepth(root.left), self.minDepth(root.right)) + 1

Information

  • Created: 2020-01-09 23:32:19

  • Last Motified: 2020-01-09 23:32:19

  • Tags: Tree, Depth-first Search Breadth-first Search