Skip to content

Practice Time Taken to Mark All Nodes

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#3241
LeetCode Problem

Time Taken to Mark All Nodes

Problem Statement

The pairs edges[i] = [u, v] connect nodes of an undirected tree, where n = edges.length + 1 and nodes are numbered 0 through n - 1. Once a neighbor is marked, an unmarked odd-numbered node takes one time unit to become marked, while an even-numbered node takes two. For each starting node i independently, mark only i at time 0 and return the time when every node has become marked. Return these times in node-index order.

Source: Time Taken to Mark All Nodes on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
edges = [[0,1],[0,2]]
Output
[2,4,3]

# Example 2

Input
edges = [[0,1]]
Output
[1,2]

# Example 3

Input
edges = [[2,4],[0,1],[2,3],[0,2]]
Output
[4,6,3,5,5]

Constraints

2 <= n <= 10^5
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.

Related LeetCode Problems

Practice more problems similar to Time Taken to Mark All Nodes.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.