PPractick

DSA interview question

Minimum Time to Burn a Binary Tree from a Node

Hard · Data structures · Topics: tree, dfs, bfs

The problem

A fire starts at one node of a binary tree, and every second it spreads from each burning node to its parent and both children. How many seconds until the whole tree is burning? That's the largest distance from the start node to any other node, since fire travels along every edge at the same speed. Record parent pointers, then BFS from the start node counting levels until every node has been reached.

Example: tree [1, 2, 3, 4, 5, 6, 7], fire starts at 2 → 3 seconds (the last nodes to burn are 6 and 7).

Function signature

int minTimeToBurn(TreeNode* root, TreeNode* start);

Test cases

Input (tree, start)OutputExplanation
([1,2,3,4,5,null,6,null,null,7,8,null,9,null,null,null,null,null,10], 8)78-5-2-1-3-6-9-10
([1, 2, 3, 4, 5, 6, 7], 2)32-1-3-6
([1, 2, 3, 4, 5, 6, 7], 1)2From the root: its height
([1, 2, null, 3, null, 4], 3)2Up to 1 takes 2 seconds, down to 4 takes 1
([1], 1)0

(Confirm: fire spreads to the parent and children each second; values are unique; the start node is in the tree.)

How interviewers phrase it

Clarifying questions to ask

  1. Does fire spread to the parent as well as the children? (Yes.)
  2. Do all edges take exactly one second? (Yes.)
  3. Is the start node given as a value or a pointer? (A value is fine when values are unique.)

Follow-ups to expect

  1. "Why is the answer a maximum distance?"
  2. "Why is BFS natural?"
  3. "One-pass DFS alternative?"
  4. "How is this related to all nodes at distance K?"
  5. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in