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.

&nbsp;
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]]


&nbsp;
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 &lt;= 1000
	to_delete contains distinct values between 1 and 1000.


# 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 traverse(self,node,par):
        if node:
            self.parent[node.val] = par
            self.traverse(node.left,node)
            self.traverse(node.right,node)
            
    def deleteNode(self,toDelete):
                node = None
                par = self.parent[toDelete]
                if par.val == toDelete:
                    node = par
                elif par.left and par.left.val == toDelete:
                    node = par.left
                elif par.right and par.right.val == toDelete:
                    node = par.right
                if node.left:  
                    self.unique[node.left] = True
                if node.right: 
                    self.unique[node.right] = True
                    
                if node in self.unique: self.unique.pop(node)
                if node != self.parent[toDelete]:
                    if par.left == node: par.left = None
                    else: par.right = None

    
    def delNodes(self, root: Optional[TreeNode], to_delete: List[int]) -> List[TreeNode]:
        self.parent = {}
        self.traverse(root,root)
        self.unique = {root:True}
        for node in to_delete:
                self.deleteNode(node)
        return self.unique
class Solution {

    HashMap<Integer, List<TreeNode>> parent_val_child_nodes_map;
    HashMap<Integer, TreeNode> child_val_parent_node_map;

    public List<TreeNode> delNodes(TreeNode root, int[] to_delete) {

        // initialize map
        parent_val_child_nodes_map = new HashMap<> ();
        child_val_parent_node_map = new HashMap<> ();

        // fill map
        dfsFillMap(root);

        // traverse to_delete to find those that do not have parent after deleting it
        List<TreeNode> res = new ArrayList<> ();

        // actually deleting nodes
        for (int delete_val : to_delete) {

            // if the node has parent
            if (child_val_parent_node_map.containsKey(delete_val)) {
                TreeNode parent_node = child_val_parent_node_map.get(delete_val);
                if (parent_node.left != null && parent_node.left.val == delete_val) {
                    parent_node.left = null;
                }

                if (parent_node.right != null && parent_node.right.val == delete_val) {
                    parent_node.right = null;
                }
            }
        }

        // add root to the list first because root has no parent
        // only if root.val is not in to_delete
        if (!Arrays.stream(to_delete).anyMatch(j -> j == root.val)) {
            res.add(root);
        }

        // add other nodes that do not have parent
        for (int delete_val : to_delete) {
            if (parent_val_child_nodes_map.containsKey(delete_val)) {
                for (int i = 0; i < parent_val_child_nodes_map.get(delete_val).size(); i++) {

                    // make sure the add node is not in to_delete
                    int child_node_val = parent_val_child_nodes_map.get(delete_val).get(i).val;
                    if (!Arrays.stream(to_delete).anyMatch(j -> j == child_node_val)) {
                        res.add(parent_val_child_nodes_map.get(delete_val).get(i));
                    }
                }
            }
        }

        return res;
    }

    public void dfsFillMap(TreeNode root) {

        if (root == null) {
            return;
        }

        if (root.left != null) {
            parent_val_child_nodes_map.putIfAbsent(root.val, new ArrayList<> ());
            parent_val_child_nodes_map.get(root.val).add(root.left);

            child_val_parent_node_map.putIfAbsent(root.left.val, root);
            dfsFillMap(root.left);
        }

        if (root.right != null) {
            parent_val_child_nodes_map.putIfAbsent(root.val, new ArrayList<> ());
            parent_val_child_nodes_map.get(root.val).add(root.right);

            child_val_parent_node_map.putIfAbsent(root.right.val, root);
            dfsFillMap(root.right);
        }

        return;
    }
}
class Solution {
    void solve(TreeNode* root,TreeNode* prev,unordered_map<int,int>& mp,int left,vector<TreeNode*>& ans){
        if(root==NULL)
            return;

        int f=0;

        solve(root->left,root,mp,1,ans);
        solve(root->right,root,mp,0,ans);

         if(mp.find(root->val)!=mp.end()){
            f=1;
            if(root->right)
                ans.push_back(root->right);
                // cout<<root->left->val<<" ";}
            if(root->left)
                ans.push_back(root->left);
                // cout<<root->right->val<<" ";}
        }

        if(f==1){
            if(left)
                prev->left=NULL;
            else
                prev->right=NULL;
        }
    }

public:
    vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) {
        if(root==NULL)
            return {};

        unordered_map<int,int> mp;
        for(int i=0;i<to_delete.size();i++)
            mp[to_delete[i]]++;

       vector<TreeNode*> ans;

        TreeNode* dummy=new TreeNode(-1);
        dummy->left=root;

        solve(dummy->left,dummy,mp,1,ans);
        if(dummy->left)
        ans.push_back(root);

        return ans;
    }
};
        ```
var delNodes = function(root, to_delete) {
    if(!root) return [];

    to_delete = new Set(to_delete);
    // know how to delete
    // while deleting add new nodes to same algo
    const ans = [];
    const traverse = (r = root, p = null, d = 0) => {
        if(!r) return null;
        if(to_delete.has(r.val)) {
            if(p != null) {
                p[d == -1 ? 'left' : 'right'] = null;
            }
            traverse(r.left, null, 0);
            traverse(r.right, null, 0);
        } else {
            if(p == null) ans.push(r);
            traverse(r.left, r, -1);
            traverse(r.right, r, 1);
        }
    }
    traverse();
    return ans;
};
Delete Nodes And Return Forest