← DiffPush

Minimum Time to Burn the Binary Tree

DiffPush Tier Binary Trees · Hard O(n) · O(n)

The Contagion Clock in the Server Farm

A corrupted update is detonated on one server blade of a tree-shaped rack network. Each second the corruption jumps to every directly connected blade — the two children and the parent. Ops needs to know the exact second the whole farm goes dark, so they can schedule the isolation window.

Input: A binary tree as a level-order array (null marks a missing child) and an integer start — the value of the initially infected node. Node values are unique.

Output: Return the number of seconds until every node is infected.

Constraints

Examples

Example 1

Input: {"tree":[1,2,3,4,5,null,6,null,null,7,8,null,9,null,null,null,null,null,10],"start":8}
Output: 7
The fire climbs 8 -> 5 -> 2 -> 1 -> 3 -> 6 -> 9 -> 10: seven hops to the farthest blade.

Example 2

Input: {"tree":[1,2,3],"start":2}
Output: 2
The parent 1 burns first, then the sibling 3 catches it one second later.

Solve this in your browser →

Also on LeetCode ↗