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.)
Example 1:
Input: root = [1,null,3,2,4,null,5,6]
Output: 3Explanation: Diameter is shown in red color.
Example 2:
Input: root = [1,null,2,null,3,4,null,5,null,6]
Output: 4Example 3:
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]
Output: 7Constraints:
1000.[1, 10⁴].The height of a node is defined as the length of the longest downward path to a leaf node from that node.
class Solution:
def diameter(self, root: 'Node') -> int:
diameter = 0
def height(node):
""" return the height of the node """
nonlocal diameter
if len(node.children) == 0:
return 0
# select the top two heights
max_height_1, max_height_2 = 0, 0
for child in node.children:
parent_height = height(child) + 1
if parent_height > max_height_1:
max_height_1, max_height_2 = parent_height, max_height_1
elif parent_height > max_height_2:
max_height_2 = parent_height
# calculate the distance between the two farthest leaves nodes.
distance = max_height_1 + max_height_2
diameter = max(diameter, distance)
return max_height_1
height(root)
return diameterWhere is the number of nodes in the tree.
The depth of a node is the length of the path to the root node.
class Solution:
def diameter(self, root: 'Node') -> int:
diameter = 0
def maxDepth(node, curr_depth):
""" return the maximum depth of leaves nodes
descending from the current node
"""
nonlocal diameter
if len(node.children) == 0:
return curr_depth
# select the top 2 depths from its children
max_depth_1, max_depth_2 = curr_depth, 0
for child in node.children:
depth = maxDepth(child, curr_depth+1)
if depth > max_depth_1:
max_depth_1, max_depth_2 = depth, max_depth_1
elif depth > max_depth_2:
max_depth_2 = depth
# calculate the distance between the two farthest leaves nodes
distance = max_depth_1 + max_depth_2 - 2 * curr_depth
diameter = max(diameter, distance)
return max_depth_1
maxDepth(root, 0)
return diameterWhere is the number of nodes in the tree.