111. 二叉树的最小深度

Easy

思路

综上,尝试写一下代码,AC!

代码

python3

class Solution:
    def minDepth(self, root: TreeNode) -> int:
      if root is None:
        return 0
      queue = [{'item':root,'depth': 1}]
      while len(queue):
        node = queue[0]['item']
        depth = queue[0]['depth']
        del queue[0]
        if node.left is None and node.right is None:
          return depth
        if node.left is not None:
          queue.append({'item':node.left,'depth': depth + 1})
        if node.right is not None:
          queue.append({'item':node.right,'depth': depth + 1})
      return 0