Skip to main content
LeetCode 1522, Medium. Topics: Tree, Depth-First Search, Tree DP. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 18 parametrized pytest cases, and a playground notebook:

Problem

Given a root of an N-ary tree, you need to compute the length of the diameter of the tree. The diameter of an N-ary tree is the length of the longest path between any two nodes in the tree. This path may or may not pass through the root. (Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value.)

Examples

Example 1
Example 2
Example 3

Constraints

  • The depth of the n-ary tree is less than or equal to 1000.
  • The total number of nodes is between [1, 10^4].

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026