Tree diameter maximum weight path

Write codeMedium25 min

Your task

Given a weighted tree with n nodes (0-indexed) as an edge list of [u, v, w], return the diameter: the maximum total weight of any simple path between two nodes. 1 <= n <= 5000, 1 <= w <= 1000. For n==1 the diameter is 0.

Examples

tree_diameter(n: int, edges: list[list[int]]) -> int
Example 1
Inputtree_diameter(4, [[0, 1, 1], [1, 2, 1], [1, 3, 1]])
Output2