Loading...
You are given an unweighted tree with n nodes labeled 1 to n, described by an array edges of its n - 1 undirected edges, where edges[i] = [a, b].
For every node, find the maximum number of edges on a path from it to any other node.
Return an array of n integers where entry i (0-based) is that maximum for node i + 1.
Input: n = 5, edges = [[1,2],[1,3],[3,4],[3,5]]
Output: [2,3,2,3,3]
Explanation: From node 1 the farthest nodes (4 or 5) are 2 edges away. From node 2 the farthest (4 or 5) is 3 edges away. From node 3 the farthest (2) is 2 edges away.
Input: n = 1, edges = []
Output: [0]
edges.length =n−1Click "Run" to test with sample cases or "Submit" to run all tests.