Forest Roots After Removing Selected Tree Nodes

Quick Overview

Forest Roots After Removing Selected Tree Nodes. Remove every node whose value is listed in to_delete from the binary tree.

Forest Roots After Removing Selected Tree Nodes

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Remove every node whose value is listed in to_delete from the binary tree. The surviving nodes and their surviving edges form a forest. Return the roots of its trees in any order, using the original native node objects. ```cpp class Solution { public: vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete); }; ``` ### Examples **Example 1:** ```text Input: root = [4,2,6,1,3], to_delete = [2] Output: forest = [[4,null,6],[1],[3]] Explanation: Removing2 separates its children1 and3 from the surviving root4. ``` **Example 2:** ```text Input: root = [7], to_delete = [7] Output: [] Explanation: No node survives. ``` ### Constraints - The tree has at most 1000 nodes; the source states no positive lower bound. - Original node values are distinct integers in `[1,1000]`. - The deletion list has at most 1000 distinct values, each in `[1,1000]`. ### Contract considerations Retain `TreeNode*` input and `vector<TreeNode*>` output. Cut links on the existing surviving nodes; array notation only illustrates tree values. ```hint Read the boundary condition What should the forest contain when every original node is removed, or when no listed value occurs? ``` ### What a Strong Answer Covers Retain `TreeNode*` input and `vector<TreeNode*>` output. Cut links on the existing surviving nodes; array notation only illustrates tree values. ### Follow-up Questions Optional practice extension, separate from the original task: How would you report the number of nodes in each surviving component?

Overview: Forest Roots After Removing Selected Tree Nodes. Remove every node whose value is listed in to_delete from the binary tree.

Google logo
Google
Jun 18, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Remove every node whose value is listed in to_delete from the binary tree. The surviving nodes and their surviving edges form a forest. Return the roots of its trees in any order, using the original native node objects.

class Solution {
public:
    vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete);
};

Examples

Example 1:

Input: root = [4,2,6,1,3], to_delete = [2]
Output: forest = [[4,null,6],[1],[3]]
Explanation: Removing2 separates its children1 and3 from the surviving root4.

Example 2:

Input: root = [7], to_delete = [7]
Output: []
Explanation: No node survives.

Constraints

  • The tree has at most 1000 nodes; the source states no positive lower bound.
  • Original node values are distinct integers in [1,1000] .
  • The deletion list has at most 1000 distinct values, each in [1,1000] .

Contract considerations

Retain TreeNode* input and vector<TreeNode*> output. Cut links on the existing surviving nodes; array notation only illustrates tree values.

What a Strong Answer Covers Guidance

Retain TreeNode* input and vector<TreeNode*> output. Cut links on the existing surviving nodes; array notation only illustrates tree values.

Follow-up Questions Guidance

Optional practice extension, separate from the original task: How would you report the number of nodes in each surviving component?