Time Taken to Mark All Nodes - Problem
Time Taken to Mark All Nodes

Imagine a network of computers connected in a tree structure where information spreads at different rates depending on the computer type. You have n computers numbered 0 to n-1 connected by n-1 direct links forming a tree.

Each computer has a unique spreading pattern:
โ€ข Even-numbered computers: Receive information every 2 time units from their neighbors
โ€ข Odd-numbered computers: Receive information every 1 time unit from their neighbors

Starting from any computer at time t=0, information spreads through the network. Your task is to determine how long it takes for all computers to receive the information when starting from each possible computer.

Goal: Return an array where times[i] represents the total time needed to mark all nodes when starting from node i.

Input & Output

example_1.py โ€” Simple Tree
$ Input: edges = [[0,1],[0,2]]
โ€บ Output: [2, 4, 2]
๐Ÿ’ก Note: When starting from node 0: Node 1 (odd) gets marked at time 1, Node 2 (even) gets marked at time 2. Max time = 2. When starting from node 1: Node 0 (even) gets marked at time 2, then node 2 gets marked at time 4. Max time = 4. When starting from node 2: Node 0 (even) gets marked at time 2, then node 1 gets marked at time 3. Max time = 3.
example_2.py โ€” Linear Tree
$ Input: edges = [[0,1],[1,2],[2,3]]
โ€บ Output: [4, 6, 6, 4]
๐Ÿ’ก Note: Linear tree where information must propagate through multiple hops. The middle nodes (1,2) require more time as they need to spread information in both directions.
example_3.py โ€” Single Node
$ Input: edges = []
โ€บ Output: [0]
๐Ÿ’ก Note: Edge case with only one node. No spreading needed, so time is 0.

Visualization

Tap to expand
01t=12t=23t=3Information Spreading PatternEven nodes (orange): 2 time units delayOdd nodes (blue/purple): 1 time unit delayMaximum time needed: 3
Understanding the Visualization
1
Initialize
Start spreading from the chosen root node at time 0
2
First Wave
Odd neighbors receive information after 1 time unit, even neighbors after 2 time units
3
Propagation
Information continues to spread outward through the tree structure
4
Completion
Record the time when the last node receives information
Key Takeaway
๐ŸŽฏ Key Insight: The tree rerooting technique allows us to compute all answers efficiently by leveraging the fact that when we change the root, only a small part of the tree structure changes, enabling O(1) updates per root transition.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

For each of the n starting positions, we perform a BFS traversal that visits all n nodes

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Space for the adjacency list, queue for BFS, and visited array

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค n โ‰ค 105
  • edges.length == n - 1
  • 0 โ‰ค ui, vi โ‰ค n - 1
  • The given input represents a valid tree
Asked in
Google 25 Amazon 18 Meta 12 Microsoft 15
28.5K Views
Medium-High Frequency
~35 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen