# Maximum Depth of N-ary Tree

Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. For example, given a`3-ary`tree:

![](https://leetcode.com/static/images/problemset/NaryTreeExample.png)

We should return its max depth, which is 3.

## Solution

```
/*
// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val,List<Node> _children) {
        val = _val;
        children = _children;
    }
};
*/
class Solution {
    int max = Integer.MIN_VALUE;

    public int maxDepth(Node root) {
        // need to find depth of all children
        if (root == null) return 0;
        return depth(root, 1);
    }

    private int depth(Node node, int depth) {
        if (node == null) {
            return depth;
        }
        int newDepth = depth + 1;
        List<Node> children = node.children;
        for (Node child : children) {
            int maxCandidate = depth(child, newDepth);
            System.out.println(maxCandidate);
            max = Math.max(max, maxCandidate);
        }

        return Math.max(max, depth);
    }
}
```


---

# 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://ondrej-kvasnovsky-2.gitbook.io/algorithms/data-structures/n-ary-tree/maximum-depth-of-n-ary-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.
