How Long Until Fire Reaches the Whole Tree

Implement burnTime

You are given the root of a binary tree with distinct node values and a value start that exists in the tree. At minute 0 the node holding start catches fire. Every minute the fire spreads from each burning node to all of its neighbours: its left child, its right child and its parent. Return how many minutes it takes until every node in the tree is on fire.

You can convert the tree to a graph and run a breadth-first search, or compute the answer in a single recursive pass by combining subtree heights with the distance from each ancestor to the start node.

Example 1:

Input: root = [15,8,22,4,11,19,30,2,6,9,13], start = 11

Output: 4

Example 2:

Input: root = [15,8,22,4,11,19,30,2,6,9,13], start = 15

Output: 3

Example 3:

Input: root = [15,8,22,4,11,19,30,2,6,9,13], start = 30

Output: 5

+ 14 hidden test cases run on Submit.

Constraints:

  • ●0 ≤ number of nodes ≤ 100; all node values are distinct, 1 ≤ node.val ≤ 1000
  • ●start is a value that exists in the tree (if the tree is empty the answer is 0)
  • ●The tree is given as its root node; each node has a val, a left child and a right child
  • ●At minute 0 the node holding start catches fire. Every minute, fire spreads from each burning node to all of its neighbours — its parent and both its children. Return the number of minutes until every node is burning (0 if the tree has a single node)

root =

[15, 8, 22, 4, 11, 19, 30, 2, 6, 9, 13]

start =

11