mediumTreesPattern: BFS

MinTreeTraversalTime Solution

Problem Statement

Given a tree with n nodes, where each node has a certain number of child nodes and a time delay associated with each child node, find the minimum time delay required for a message to reach all nodes in the network.

Examples

Example 1:
Input:Root node (0) has 2 children with child delay (2) and child delay (4).
Output:8
Explanation: We traverse the tree, adding the delay of each child node to get the minimum time delay.
Example 2:
Input:The tree contains 5 nodes as shown: (0 -> [1, 2], 1 -> [3, 4]). All nodes have equal delay.
Output:2
Explanation: The minimum time delay is the sum of the delays of all nodes divided by 2.
Example 3:
Input:The tree is empty (n=0).
Output:0
Explanation: There is no delay when traversing an empty tree.

Constraints

  • The tree has n nodes with n-1 edges.
  • Time delays are non-negative integers.
  • The tree is connected (there is a path between every pair of nodes).
Time: O(n*depth) Space: O(n)
Implement a level-order traversal to track time delay.

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.