Class Solution
-
- All Implemented Interfaces:
public final class Solution
3241 - Time Taken to Mark All Nodes.
Hard
There exists an undirected tree with
n
nodes numbered0
ton - 1
. You are given a 2D integer arrayedges
of lengthn - 1
, where <code>edgesi = u<sub>i</sub>, v<sub>i</sub></code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree.Initially, all nodes are unmarked. For each node
i
:If
i
is odd, the node will get marked at timex
if there is at least one node adjacent to it which was marked at timex - 1
.If
i
is even, the node will get marked at timex
if there is at least one node adjacent to it which was marked at timex - 2
.
Return an array
times
wheretimes[i]
is the time when all nodes get marked in the tree, if you mark nodei
at timet = 0
.Note that the answer for each
times[i]
is independent , i.e. when you mark nodei
all other nodes are unmarked.Example 1:
Input: edges = [0,1,0,2]
Output: 2,4,3
Explanation:
For
i = 0
:For
i = 1
:For
i = 2
:
Example 2:
Input: edges = [0,1]
Output: 1,2
Explanation:
For
i = 0
:For
i = 1
:
Example 3:
Input: edges = [2,4,0,1,2,3,0,2]
Output: 4,6,3,5,5
Explanation:
Constraints:
<code>2 <= n <= 10<sup>5</sup></code>
edges.length == n - 1
edges[i].length == 2
0 <= edges[i][0], edges[i][1] <= n - 1
The input is generated such that
edges
represents a valid tree.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-