---
# System prepended metadata

title: CSPT13 Build Week 2

---

# CSPT13 Build Week 2
```
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right


"""
Understand

3
output: 1

    3
 9     20
     15   7
output: 2

3
   20
      7
output: 3

Plan

Use BFS to find the closest shallow leaf. Keep track of the current level of the node
Once you encounter a leaf, output its level

"""
from collections import deque

class Solution(object):
    def minDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if root == None:
            return 0
        queue = deque()
        queue.append((root, 1))
        while len(queue) > 0:
            curr = queue.popleft()
            currNode, currLevel = curr[0], curr[1]
            if currNode.left == None and currNode.right == None:
                return currLevel
            if currNode.left != None:
                queue.append((currNode.left, currLevel + 1))
            if currNode.right != None:
                queue.append((currNode.right, currLevel + 1))
        return -1
        
        
        
        
        
```