# 55. N叉树的最大深度

java

```java
package leetcode.all.solution501_600;

import java.util.ArrayList;
import java.util.List;

/**
 * 559. N叉树的最大深度
 *
 * 给定一个N叉树，找到其最大深度。
 *
 * 最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。
 *
 * 说明:
 * 树的深度不会超过 1000。
 * 树的节点总不会超过 5000。
 */
public class Solution559 {
    public int maxDepth(Node root) {
        if(root==null) return 0;
        if(root.children==null) return 1;
        int max=0;
        for(Node node:root.children){
            int t=maxDepth(node);
            if(t>max) max=t;
        }
        return max+1;
    }

    public static void main(String[] args){
        Node node5=new Node(5,null);
        Node node6=new Node(6,null);
        List<Node> children3=new ArrayList<>();
        children3.add(node5);
        children3.add(node6);

        Node node3=new Node(3,children3);
        Node node2=new Node(2,null);
        Node node4=new Node(4,null);

        List<Node> children1=new ArrayList<>();
        children1.add(node3);
        children1.add(node2);
        children1.add(node4);
        Node root=new Node(1,children1);

        int r=new Solution559().maxDepth(root);
        System.out.println(r);
    }
}
```


---

# 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/55-n-cha-shu-de-zui-da-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.
