Burning Tree

Hard Trees & BST Child + Ancestor Handling Original on GeeksforGeeks

Burning Tree is a hard trees & bst problem solved with the child + ancestor handling pattern. The best approach, one dfs (child + ancestor handling), runs in O(n) time and O(h) space. Below are 2 approaches in Java, from parent map + bfs up.

Problem

A fire starts at the node with value target. Each second it spreads to that node’s parent and children. Return the minimum time for the whole tree to burn.

Examples

Example 1

Input
root = [1, 2, 3, 4, 5, null, 6], target = 4
Output
4
Why
Second 1: 2. Second 2: 1 and 5. Second 3: 3. Second 4: 6.

Example 2

Input
root = [1, 2, 3], target = 1
Output
1

Constraints

  • The tree has 1 to 10^5 nodes; target exists.
  • Fire spreads from a burning node to its parent and children each second.

Animated walkthrough

A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the Java code.

ApproachTimeSpace
Parent map + BFSO(n)O(n)
One DFS (child + ancestor handling)O(n)O(h)

1Parent map + BFS

TimeO(n)
SpaceO(n)

Record parents to treat the tree as an undirected graph. BFS from the target and count levels.

  1. Build a parent map and locate target.
  2. BFS over children and parent; count the levels after the first.
Java
class Solution {
    public static int minTime(TreeNode root, int target) {
        Map<TreeNode, TreeNode> parent = new HashMap<>();
        TreeNode src = null;
        Queue<TreeNode> q = new ArrayDeque<>();
        q.add(root);
        while (!q.isEmpty()) {
            TreeNode n = q.poll();
            if (n.val == target) src = n;
            if (n.left != null) { parent.put(n.left, n); q.add(n.left); }
            if (n.right != null) { parent.put(n.right, n); q.add(n.right); }
        }
        Set<TreeNode> burnt = new HashSet<>();
        q.add(src);
        burnt.add(src);
        int time = -1;
        while (!q.isEmpty()) {
            time++;
            for (int i = q.size(); i > 0; i--) {
                TreeNode n = q.poll();
                for (TreeNode nb : new TreeNode[] { n.left, n.right, parent.get(n) })
                    if (nb != null && burnt.add(nb)) q.add(nb);
            }
        }
        return time;
    }
}

2One DFS (child + ancestor handling)

TimeO(n)
SpaceO(h)

Return subtree height normally, but a negative distance when the subtree contains the target. At each ancestor, the fire reaches the far side after (distance to target) + (height of the other subtree) seconds. At the target itself, the answer is its own height below.

  1. At the target: best = max(best, height below); return -1.
  2. Ancestor with a negative side: best = max(best, dist + other height); return -(dist + 1).
  3. Otherwise return 1 + max(l, r).
Java
class Solution {
    private static int best;

    public static int minTime(TreeNode root, int target) {
        best = 0;
        dfs(root, target);
        return best;
    }

    private static int dfs(TreeNode n, int target) {
        if (n == null) return 0;
        int l = dfs(n.left, target), r = dfs(n.right, target);
        if (n.val == target) {
            best = Math.max(best, Math.max(l, r));
            return -1;
        }
        if (l >= 0 && r >= 0) return 1 + Math.max(l, r);
        int dist = -Math.min(l, r);
        int other = l >= 0 ? l : r;
        best = Math.max(best, dist + other);
        return -(dist + 1);
    }
}

Edge cases to test

  • Target is the root
  • Target is a leaf on the longest branch

Hints

Hint 1

Same problem as Amount of Time for Binary Tree to Be Infected: the answer is the farthest node's distance from the target.

FAQ

What is the best time complexity for Burning Tree?

One DFS (child + ancestor handling) runs in O(n) time and O(h) extra space.

Which pattern does Burning Tree use?

It is a trees & bst problem that uses the child + ancestor handling pattern. Other problems with the same pattern: Amount of Time for Binary Tree to Be Infected, All Nodes Distance K in Binary Tree.

Is there a brute force solution for Burning Tree?

Yes. Parent map + BFS takes O(n) time and O(n) space. Record parents to treat the tree as an undirected graph.

Which edge cases should I test for Burning Tree?

Target is the root; Target is a leaf on the longest branch.