Coding questions · Tesla
Compute time to burn tree
This question evaluates proficiency with tree algorithms, distance computation, and propagation modeling, testing the ability to reason about how a process spreads across parent-child relationships in a tree.
Given the root of a binary tree and the value of a target node that starts burning at time 0, fire spreads every minute from a burning node to its left child, right child, and parent.
Return the number of minutes required for the entire tree to burn.
Notes:
- All node values are unique.
- The tree is not necessarily balanced.
- If the tree has only the target node, the answer is
0.
Example idea: if the farthest node from the target is 4 edges away, the tree burns completely in 4 minutes.
Compute Time to Burn Tree
You are given a binary tree and the value of a target node that starts burning at time 0. Every minute, fire spreads from each burning node to its left child, right child, and parent.
For this problem, the tree is provided as a level-order list tree, where None represents a missing node. If a node is stored at index i, its left child is at 2*i + 1 and its right child is at 2*i + 2, as long as those positions exist and are not None.
Return the number of minutes required for the entire tree to burn.
All non-None node values are unique. If the tree contains only the target node, the answer is 0.
Example: if the farthest node from the target is 4 edges away, the whole tree burns in 4 minutes.
Constraints
1 <= len(tree) <= 10^5tree[0]is notNone- Each entry in
treeis either an integer orNone - All non-
Nonenode values are unique targetis guaranteed to be one of the non-Nonevalues intree
Hints
- If you can move from a node to its parent as well as its children, the tree behaves like an undirected graph.
- Build the parent/adjacency relationships first, then run BFS from the target one minute at a time to find the farthest reachable node.