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.)
from __future__ import annotationsclass NaryNode: def __init__(self, val: int = 0, children: list[NaryNode] | None = None) -> None: self.val = val self.children = children if children is not None else []class Solution: # Time: O(n) # Space: O(h) def diameter(self, root: NaryNode | None) -> int: ans = 0 def dfs(node: NaryNode | None) -> int: nonlocal ans if node is None: return 0 first = second = 0 for child in node.children: depth = dfs(child) if depth > first: second, first = first, depth elif depth > second: second = depth ans = max(ans, first + second) return 1 + first dfs(root) return ans