# 1110. Delete Nodes And Return Forest

## Description

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:

• The number of nodes in the given tree is at most 1000.
• Each node has a distinct value between 1 and 1000.
• to_delete.length <= 1000
• to_delete contains distinct values between 1 and 1000.

## Solutions

Solution 1: DFS

First, we use a hash table or an array of length 1001, s, to record all nodes that need to be deleted.

Next, we design a function dfs(root) that returns the root of the subtree with root as the root after deleting all nodes that need to be deleted. The execution logic of the function dfs(root) is as follows:

• If root is null, we return null;
• Otherwise, we recursively execute dfs(root.left) and dfs(root.right), and assign the return values to root.left and root.right respectively. If root does not need to be deleted, we return root; if root needs to be deleted, we check whether root.left and root.right are null. If they are not null, we add them to the answer array; finally, we return null.

In the main function, we call dfs(root). If the result is not null, it means that the root node does not need to be deleted, and we add the root node to the answer array. Finally, we return the answer array.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes in the tree.

• /**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode() {}
*     TreeNode(int val) { this.val = val; }
*     TreeNode(int val, TreeNode left, TreeNode right) {
*         this.val = val;
*         this.left = left;
*         this.right = right;
*     }
* }
*/
class Solution {
private boolean[] s = new boolean[1001];
private List<TreeNode> ans = new ArrayList<>();

public List<TreeNode> delNodes(TreeNode root, int[] to_delete) {
for (int x : to_delete) {
s[x] = true;
}
if (dfs(root) != null) {
}
return ans;
}

private TreeNode dfs(TreeNode root) {
if (root == null) {
return null;
}
root.left = dfs(root.left);
root.right = dfs(root.right);
if (!s[root.val]) {
return root;
}
if (root.left != null) {
}
if (root.right != null) {
}
return null;
}
}

• /**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) {
bool s[1001];
memset(s, 0, sizeof(s));
for (int x : to_delete) {
s[x] = true;
}
vector<TreeNode*> ans;
function<TreeNode*(TreeNode*)> dfs = [&](TreeNode* root) -> TreeNode* {
if (!root) {
return nullptr;
}
root->left = dfs(root->left);
root->right = dfs(root->right);
if (!s[root->val]) {
return root;
}
if (root->left) {
ans.push_back(root->left);
}
if (root->right) {
ans.push_back(root->right);
}
return nullptr;
};
if (dfs(root)) {
ans.push_back(root);
}
return ans;
}
};

• # Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
def delNodes(
self, root: Optional[TreeNode], to_delete: List[int]
) -> List[TreeNode]:
def dfs(root: Optional[TreeNode]) -> Optional[TreeNode]:
if root is None:
return None
root.left, root.right = dfs(root.left), dfs(root.right)
if root.val not in s:
return root
if root.left:
ans.append(root.left)
if root.right:
ans.append(root.right)
return None

s = set(to_delete)
ans = []
if dfs(root):
ans.append(root)
return ans


• /**
* Definition for a binary tree node.
* type TreeNode struct {
*     Val int
*     Left *TreeNode
*     Right *TreeNode
* }
*/
func delNodes(root *TreeNode, to_delete []int) (ans []*TreeNode) {
s := make([]bool, 1001)
for _, x := range to_delete {
s[x] = true
}
var dfs func(*TreeNode) *TreeNode
dfs = func(root *TreeNode) *TreeNode {
if root == nil {
return nil
}
root.Left = dfs(root.Left)
root.Right = dfs(root.Right)
if !s[root.Val] {
return root
}
if root.Left != nil {
ans = append(ans, root.Left)
}
if root.Right != nil {
ans = append(ans, root.Right)
}
return nil
}
if dfs(root) != nil {
ans = append(ans, root)
}
return
}

• /**
* Definition for a binary tree node.
* class TreeNode {
*     val: number
*     left: TreeNode | null
*     right: TreeNode | null
*     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
*         this.val = (val===undefined ? 0 : val)
*         this.left = (left===undefined ? null : left)
*         this.right = (right===undefined ? null : right)
*     }
* }
*/

function delNodes(root: TreeNode | null, to_delete: number[]): Array<TreeNode | null> {
const s: boolean[] = Array(1001).fill(false);
for (const x of to_delete) {
s[x] = true;
}
const ans: Array<TreeNode | null> = [];
const dfs = (root: TreeNode | null): TreeNode | null => {
if (!root) {
return null;
}
root.left = dfs(root.left);
root.right = dfs(root.right);
if (!s[root.val]) {
return root;
}
if (root.left) {
ans.push(root.left);
}
if (root.right) {
ans.push(root.right);
}
return null;
};
if (dfs(root)) {
ans.push(root);
}
return ans;
}