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
graph TD A(1) A --- B(3) A --- C(2) A --- D(4) B(3) --- E(5) B(3) --- F(6) linkStyle 0 stroke:#FF0000,stroke-width:2px; linkStyle 1 stroke:#FF0000,stroke-width:2px; linkStyle 3 stroke:#FF0000,stroke-width:2px;
|
|
Example 2
graph TD A(1) --- B(2) B --- C(3) & D(4) C --- E(5) D --- F(6) classDef hidden display:none linkStyle 1 stroke:#FF0000,stroke-width:2px; linkStyle 2 stroke:#FF0000,stroke-width:2px; linkStyle 3 stroke:#FF0000,stroke-width:2px; linkStyle 4 stroke:#FF0000,stroke-width:2px;
|
|
Example 3
graph TD A(1) A --- B(2) & C(3) & D(4) & E(5) C --- F(6) & G(7) D --- H(8) --- L(12) G --- K(11) --- N(14) E --- I(9) & J(10) I --- M(13) linkStyle 1 stroke:#FF0000,stroke-width:2px; linkStyle 2 stroke:#FF0000,stroke-width:2px; linkStyle 5 stroke:#FF0000,stroke-width:2px; linkStyle 6 stroke:#FF0000,stroke-width:2px; linkStyle 8 stroke:#FF0000,stroke-width:2px; linkStyle 9 stroke:#FF0000,stroke-width:2px; linkStyle 7 stroke:#FF0000,stroke-width:2px;
|
|
Similar Problem
Solution
Definition of Diameter: The diameter of the tree is the longest path between any two nodes. This path may or may not pass through the root. Hence, we need to compute the longest paths in the subtree of each node.
Method 1 - DFS
We can use a recursive function to traverse through child nodes and calculate the heights.
Here is the approach:
- Start from the root and recursively calculate the heights of the subtrees of all child nodes.
- The height of a node is defined as the longest path from the node to any leaf.
- For each node, calculate the longest two heights among its children (let’s call them
h1
andh2
). - The local diameter at a node will be
h1 + h2
. - Update the global diameter (
ans
) to be the maximum of the current global diameter and the local diameter.
- pick any node u
- find the node which is farthest from u, call it x (calculate using the same approach as in Solution 1)
- find the node which is farthest from x, call it q (calculate using the same approach as in Solution 1)
The answer will be the length of a path from x to q.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
- Each node of the tree is visited exactly once. Calculating heights and updating the global diameter also takes constant time per node. Hence, the traversal takes
O(n)
.
- Each node of the tree is visited exactly once. Calculating heights and updating the global diameter also takes constant time per node. Hence, the traversal takes
- 🧺 Space complexity:
O(h)
- The recursion stack can go as deep as the height
h
of the tree in the worst case. Therefore, the space complexity isO(h)
.
- The recursion stack can go as deep as the height