> For the complete documentation index, see [llms.txt](https://emmaguo100.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emmaguo100.gitbook.io/leetcode/04-06-2022-111.-minimum-depth-of-binary-tree.md).

# 04/06/2022 111. Minimum Depth of Binary Tree

Method: recusion. Different from maxmium depthh of binary tree. For the minimum depth of binary tree. if left subtree is null, we need to check right subtree depth + 1 and vice versa when the right subtree is null. When they are not null, we should get min of them + 1.

Time O(N)

Space O(H)

```
// Some code
class Solution {
    public int minDepth(TreeNode root) {
        if (root == null) return 0;
        int leftDepth = minDepth(root.left);
        int rightDepth = minDepth(root.right);
        if (leftDepth == 0 && rightDepth == 0) return 1;
        if (leftDepth == 0) return rightDepth + 1;
        if (rightDepth == 0) return leftDepth + 1; 
        return Math.min(leftDepth, rightDepth) + 1;
        
    }
}
```

Method 2:

Use BFS. use queue to add each level of node and increment depth. When we arrive at the leaf node, we just return the depth.

Time O(N)

Space O(N)

```
// Some code
 */
class Solution {
    public int minDepth(TreeNode root) {
        if (root == null) return 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int depth = 0;
        while (!queue.isEmpty()){
            int size = queue.size();
            depth++;
            for (int i = 0; i < size; i++){
                TreeNode curr = queue.poll();
                if (curr.left == null && curr.right == null) return depth;
                if (curr.left != null) queue.offer(curr.left);
                if (curr.right != null) queue.offer(curr.right);
            }
        }
        return depth;
    }
}
```
