Watch 10 video solutions for Delete Nodes And Return Forest, a medium level problem involving Array, Hash Table, Tree. This walkthrough by Kevin Naughton Jr. has 31,446 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given the root of a binary tree, each node in the tree has a distinct value.
After deleting all nodes with a value in to_delete, we are left with a forest (a disjoint union of trees).
Return the roots of the trees in the remaining forest. You may return the result in any order.
Example 1:
Input: root = [1,2,3,4,5,6,7], to_delete = [3,5] Output: [[1,2,null,4],[6],[7]]
Example 2:
Input: root = [1,2,4,null,3], to_delete = [3] Output: [[1,2,4]]
Constraints:
1000.1 and 1000.to_delete.length <= 1000to_delete contains distinct values between 1 and 1000.Problem Overview: Given a binary tree and a list of node values to delete, remove those nodes and return the roots of the remaining trees (the forest). When a node is deleted, its non-deleted children become new roots in the resulting forest.
Approach 1: Post-order Traversal (O(n) time, O(h) space)
This approach uses Depth-First Search with a post-order traversal. You process the left and right children before the current node, which ensures you already know whether a child was deleted before deciding how to handle the parent. Store all values from to_delete in a hash table for O(1) lookup. During recursion, if the current node must be deleted, push its non-null children into the forest list and return null to detach it from its parent. If the node stays, return it normally. The root requires special handling: if it is not deleted, add it to the forest at the start. This method touches each node exactly once, giving O(n) time and O(h) recursion stack space where h is the tree height.
The key insight is that deletions affect parent-child links. Post-order traversal lets you resolve children first, so by the time you evaluate a node you already know whether its subtrees survived or were removed. This prevents dangling references and keeps the logic compact.
Approach 2: BFS with Parent References (O(n) time, O(n) space)
This approach performs a level-order traversal using a queue on the Binary Tree. Along with each node, track its parent and whether it is a left or right child. Again, keep a hash set of nodes to delete for constant-time checks. When visiting a node marked for deletion, detach it from its parent and push its children into the forest if those children are not also scheduled for deletion. Continue BFS until every node is processed.
This strategy works well when you prefer iterative traversal instead of recursion. Because the queue may hold many nodes at once, the auxiliary memory can reach O(n) in the worst case. The logic is slightly more verbose because you explicitly update parent pointers during deletion, but the algorithm still processes each node only once.
Recommended for interviews: The post-order DFS approach is what interviewers usually expect. It shows clear understanding of tree traversal order and how structural modifications propagate upward in recursion. BFS with parent references is also valid, but the DFS solution is shorter, easier to reason about, and demonstrates stronger control over recursive tree manipulation.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Post-order Traversal (DFS) | O(n) | O(h) | Best general solution for tree deletion problems. Preferred in interviews due to clean recursion and minimal extra memory. |
| BFS with Parent References | O(n) | O(n) | Useful when avoiding recursion or when implementing iterative tree traversal with explicit parent tracking. |