Delete Nodes And Return Forest

Post-order DFS — a node whose value is to delete returns null and pushes its non-null children as roots.

Medium
Time O(n) Space O(h)
LeetCode
3 min read
tree dfs

Problem#

Given the root of a binary tree with distinct values and a list of values to delete, return all the roots of the resulting forest.

Examples#

  • root = [1,2,3,4,5,6,7], to_delete = [3,5] → roots of [1,2,null,4] and [6] and [7].
  • root = [1,2,4,null,3], to_delete = [3] → roots of [1,2,4].

Constraints#

  • The number of nodes is in [1, 1000]. to_delete.length <= 1000.

Approach#

Post-order DFS. A helper takes a flag isRoot (true at start). If the node’s value is to delete, its non-null children become roots and we return null. Otherwise, if isRoot, add this node to the forest; recurse with isRoot corresponding to “parent was deleted”.

Solution#

Delete Nodes And Return Forest
class Solution {
public:
vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) {
unordered_set<int> bad(to_delete.begin(), to_delete.end());
vector<TreeNode*> forest;
function<TreeNode*(TreeNode*, bool)> dfs = [&](TreeNode* u, bool isRoot) -> TreeNode* {
if (!u) return nullptr;
bool del = bad.count(u->val);
if (isRoot && !del) forest.push_back(u);
u->left = dfs(u->left, del);
u->right = dfs(u->right, del);
return del ? nullptr : u;
};
dfs(root, true);
return forest;
}
};

Editorial#

A node is the root of a new tree iff (a) it’s not deleted and (b) its parent was either absent (real root) or deleted. The isRoot flag plus current del decision captures both states. Children process the post-order rewrite — surviving subtree roots get reattached.

Complexity#

  • Time: O(n).
  • Space: O(h).

Concept revision#

Search ESC

Keyboard shortcuts

Shortcuts are disabled while typing in inputs.