Given a node, how long will it take to burn the whole binary tree?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
The "burning tree" problem asks: given a binary tree and a starting node (the fire source), how many seconds does it take for fire to spread to every node, if fire spreads to all adjacent nodes (parent, left child, right child) each second? This is equivalent to finding the farthest node from the source — the answer is the maximum distance from the source to any node in the tree.
Example
Starting fire at node 4:
- Time 0: Node 4 is on fire
- Time 1: Node 2 catches fire (parent of 4)
- Time 2: Nodes 1 and 5 catch fire
- Time 3: Node 3 catches fire
Answer: 3 seconds
Approach: BFS from Source Node
The key insight is that fire spreads like BFS (breadth-first search) from the source. But a binary tree only has child pointers, not parent pointers. So we first build a parent map, then BFS from the source in all directions.
Step 1: Map Each Node to Its Parent
Step 2: BFS from the Source
Complete Solution
Complexity
- Time: O(n) — BFS visits each node once, parent map construction is O(n)
- Space: O(n) — parent map stores n-1 entries, visited set stores n entries, queue holds at most O(n)
Alternative: DFS Approach
You can also solve this with a single DFS that returns the depth from the target:
This avoids building the parent map and uses O(h) stack space where h is the tree height.
Java Implementation
Common Pitfalls
- Forgetting to visit parent: A binary tree only has child pointers. Without building a parent map (or using the DFS approach), you cannot spread fire upward, giving an incorrect answer.
- Not marking visited nodes: Without a visited set, BFS revisits nodes through the parent pointer, causing an infinite loop.
- Level-based vs node-based BFS: To get the time (number of seconds), process the BFS level by level. Using the
(node, time)tuple approach shown above also works. - Target not found: If the target node does not exist in the tree, handle the edge case. The function should return -1 or raise an error.
- Single-node tree: If the tree has only the root and the root is the target, the answer is 0 (already burning).
Summary
- The burning tree problem equals finding the maximum distance from the source node to any other node
- Build a parent map (O(n)), then BFS from the source in all three directions (left, right, parent)
- Time complexity: O(n), Space complexity: O(n)
- Alternative DFS approach avoids the parent map with O(h) space
- The answer is the number of BFS levels minus 1 (or the maximum time recorded during traversal)
Related reading
- Given a permutation''s lexicographic number, is it possible to get any item in it in O1
- Given a set of points, how do I find the two points that are farthest from each other?
- Given a set of rectangles, do any overlap?
- Given a sorted array, can we build a sorted array of the sums of all pairs in On2?
- Given a sorted integer array, how may Binary Search trees can be formed from it?
- Given a tensor flow model graph, how to find the input node and output node names
- Given a string, find two identical subsequences with consecutive indexes C
- Given a string of a million numbers, return all repeating 3 digit numbers

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.