Pattern visualizer
Minimum Time to Burn Binary Tree
Fire spreads to ALL adjacent nodes each minute, and adjacency here means left child, right child, AND parent — a plain top-down traversal only reaches descendants. So the tree is first converted into an undirected graph by recording each node's parent while locating the start. Then a round-based BFS radiates outward: every round, every currently-burning node checks its three neighbors, and any unburned one catches fire. The minute counter only advances on a round that actually ignites a new node — a round that finds nothing new means the whole tree is already burning, and the loop can stop without counting that round. Animated on: Tree [1, 2, 3, 4, 5, 6, 7], start = 3 — find the minimum time (minutes) for fire starting at node 3 to reach every node..
Multi-source BFS over the tree treated as an undirected graph
Locate the start node 3 and record every node's parent while walking down (path from root: 1 -> 3). The tree is now an undirected graph — fire can spread through left, right, AND parent edges, not just downward.
1FUNCTION minTimeToBurn(root, start):2 parent <- MAP()3 target <- LOCATE(root, start, parent)4 visited <- SET(target)5 frontier <- [target]6 time <- 07 WHILE frontier NOT EMPTY:8 next <- []9 FOR EACH node IN frontier:10 FOR EACH nb IN {node.left, node.right, parent[node]}:11 IF nb != null AND nb NOT IN visited: ADD nb TO visited, APPEND nb TO next12 IF LENGTH(next) > 0: time <- time + 113 frontier <- next14 RETURN time
← / → step · space play · Home restart