Minimum Time to Burn Tree

Hard
tree bfs graph-traversal burning-simulation
Problem Description

Given a binary tree and a starting node, calculate the minimum time to burn the entire tree. Fire spreads from the starting node to all connected nodes (parent and children) each second. Return the total time needed.

Input Format
First line: n (number of nodes). Second line: starting node value. Next n-1 lines: parent-child relationships as "parent child".
Output Format
Minimum time in seconds to burn the entire tree.
Constraints
• 1 ≤ n ≤ 1000
• Node values are unique integers 1 to n
• Tree is connected (n-1 edges)
• Fire spreads to adjacent nodes each second
• Starting node burns at time 0
Sample Input/Output
Input:
7
3
1 2
1 3
2 4
2 5
3 6
3 7
Output:
3
Explanation

Tree structure with start=3: Fire spreads from 3 to 1,6,7 (time 1), then to 2 (time 2), then to 4,5 (time 3). Total time: 3.

Solution Hints
160
Points
3000ms
Time Limit
256MB
Memory Limit
Code Editor
Register to Submit
Register to Access Code Editor

Create a free account to solve coding problems and track your progress.

Register Now
Feedback