# 31. 二叉树的最小深度

java

```java
// 链表工具类
public class LinkedUtils {
    public static ListNode arrayToLinkedList(int[] array){
        if(array==null) return null;
        //head node 
        ListNode root=new ListNode(-1);
        ListNode p=root;

        int size=array.length;
        int i=0;
        while(i<size){
            ListNode q=new ListNode(array[i++]);
            q.next=null;
            p.next=q;
            p=q;
        }

        return root.next;
    }

    public static void print(ListNode root) {
        if(root==null) System.out.println("root is null");
        else{
            while(root!=null){
                System.out.print(root.val);
                if(root.next!=null){
                    System.out.print("->");
                }
                root=root.next;
            }
            System.out.println();
        }
    }

    public static int length(ListNode root) {
        if(root==null) return 0;
        int n=0;
        while(root!=null){
            root=root.next;
            n++;
        }
        return n;
    }

    // 返回指向链表尾部的指针
    public static ListNode moveToTail(ListNode root) {
        if(root==null) return null;
        ListNode tail=root;
        while(tail.next!=null){
            tail=tail.next;
        }
        return tail;
    }
}


// 定义链表
public class ListNode {
    public int val;
    public ListNode next;

    public ListNode(int val) {
        this.val=val;
    }
}


/**
 * 111. 二叉树的最小深度
 *
 * 给定一个二叉树，找出其最小深度。
 *
 * 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
 *
 * 说明: 叶子节点是指没有子节点的节点。
 *
 * 示例:
 *
 * 给定二叉树 [3,9,20,null,null,15,7],
 *
 *     3
 *    / \
 *   9  20
 *     /  \
 *    15   7
 * 返回它的最小深度  2.
 */
public class Solution111 {
    public int minDepth(TreeNode root) {
        if(root==null) return 0;
        if(root.left==null&&root.right==null) return 1;
        if(root.left==null) return minDepth(root.right)+1;
        else if(root.right==null) return minDepth(root.left)+1;
        return Math.min(minDepth(root.left),minDepth(root.right))+1;
    }

    public static void main(String[] args){
        TreeNode root=TreeUtils.stringToTree("124###3#5##");
        int min=new Solution111().minDepth(root);
        System.out.println(min);
    }
}
```

python

```
```


---

# Agent Instructions: 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://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/shua-ti/shu-ju-jie-gou-lei/31-er-cha-shu-de-zui-xiao-shen-du.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.
