397. Diameter of N-ary TreePOTD

Given the root of an N-ary tree, compute the length of its diameter.

The diameter is defined as the length of the longest path between any two nodes in the tree. (Nary-Tree input serialization is represented in their level order traversal, with each group of children separated by a null value.)

Example 1:

Input: root = [1,null,3,2,4,null,5,6]

Output: 3

Explanation: The diameter of the N-ary tree is the longest path between any two nodes, which in this case is 3.

Example 2:

Input: root = [1,null,2,null,3,4,null,5,null,6]

Output: 4

Explanation: The diameter of the N-ary tree is the longest path between any two nodes, which in this case is 4.

Now Your Turn!

Pick the correct output for the given input

Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

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

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
/**
* Definition for an n-ary tree node.
* class TreeNode {
* public:
* int val;
* vector<TreeNode*> children;
*
* TreeNode() {}
*
* TreeNode(int _val) {
* val = _val;
* }
*
* TreeNode(int _val, vector<TreeNode*> _children) {
* val = _val;
* children = _children;
* }
* };
**/
 
 
class Solution {
public:
int diameter(TreeNode* root) {
}
};
 
Test Case

Input:

Root