> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Delete Nodes And Return Forest Python Solution

> Tested Python solution for LeetCode 1110 with 13 pytest cases. Generate a practice environment with lcpy.

LeetCode 1110, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Tree](/catalog/topics/tree), [Depth-First Search](/catalog/topics/depth-first-search), [Binary Tree](/catalog/topics/binary-tree). [View on LeetCode](https://leetcode.com/problems/delete-nodes-and-return-forest/description/).

Generate this problem as a practice environment: tested reference solution, 13 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1110   # by problem number
lcpy gen -s delete_nodes_and_return_forest   # by problem name
```

## Problem

Given the \<code>root\</code> of a binary tree, each node in the tree has a \<strong>distinct\</strong> value.

After deleting all nodes with a value in \<code>to\_delete\</code>, we are left with a forest (a disjoint union of trees).

Return the roots of the trees in the remaining forest. You may return them in any order.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2019/07/01/screen-shot-2019-07-01-at-53836-pm.png)

```
Input: root = [1,2,3,4,5,6,7], to_delete = [3,5]
Output: [[1,2,null,4],[6],[7]]
```

```
Input: root = [1,2,4,null,3], to_delete = [3]
Output: [[1,2,4]]
```

### Constraints

* The number of nodes in the given tree is at most \<code>1000\</code>.
* Each node has a \<strong>distinct\</strong> value between \<code>1\</code> and \<code>1000\</code>.
* \<code>to\_delete.length \<= 1000\</code>
* \<code>to\_delete\</code> contains distinct values between \<code>1\</code> and \<code>1000\</code>.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/delete_nodes_and_return_forest/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/delete_nodes_and_return_forest/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from leetcode_py import TreeNode


class Solution:
    # Time: O(n)
    # Space: O(n)
    def del_nodes(self, root: TreeNode[int] | None, to_delete: list[int]) -> list[TreeNode[int]]:
        delete = set(to_delete)
        forest: list[TreeNode[int]] = []

        def dfs(node: TreeNode[int] | None, is_root: bool) -> TreeNode[int] | None:
            if node is None:
                return None
            deleted = node.val in delete
            if is_root and not deleted:
                forest.append(node)
            node.left = dfs(node.left, deleted)
            node.right = dfs(node.right, deleted)
            return None if deleted else node

        dfs(root, True)
        return forest
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.