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

# 111. Minimum Depth of Binary Tree

<https://leetcode.com/problems/minimum-depth-of-binary-tree/>

Given a binary tree, find its minimum depth.

The minimum depth is \*\***the number of nodes** along the shortest path from the root node down to the nearest leaf node.

**Note:** A leaf is a node with no children.

注意是節點數！

cal sub tree node -> postorder

{% embed url="<https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/254240/Readable-code-from-the-definition-of-the-minimum-depth>" %}

因為有可能這樣一個很歪斜的樹, 所以如果用 dfs 要考慮這個狀況

![](/files/rExP3bZX2bmiLKKWyhQi)

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

答案是 3, 9 -> 2 (2 node

![](/files/gzif4ZCwjtANEFznKGrV)

### use BFS

````java
```java
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    class Step {
        TreeNode node;
        int level; 
        Step(TreeNode node, int level) {
            this.node = node;
            this.level = level;
        }
    }
    public int minDepth(TreeNode root) {
        Queue<Step> queue = new LinkedList<>();
        if (root == null) {
            return 0;
        }
        queue.offer(new Step(root, 0));

        int min = Integer.MAX_VALUE;
        while (!queue.isEmpty()) {
            Step cur = queue.poll();
            TreeNode node = cur.node;
            if (node == null) {
                continue;
            }
            if (node.left == null && node.right == null) {
                min = Math.min(min, cur.level+1);
            }
            queue.offer(new Step(node.left, cur.level+1));
            queue.offer(new Step(node.right, cur.level+1));
        }
        return min;
    }
}
```
````


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/tree/111.-minimum-depth-of-binary-tree.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
