Simple Java || C++ Code ☠️
java []\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     int val;\n *     TreeNode left;\n *     TreeNode right;\n *     TreeNode
```java []\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     int val;\n *     TreeNode left;\n *     TreeNode right;\n *     TreeNode() {}\n *     TreeNode(int val) { this.val = val; }\n *     TreeNode(int val, TreeNode left, TreeNode right) {\n *         this.val = val;\n *         this.left = left;\n *         this.right = right;\n *     }\n * }\n */\nclass Solution {\n    public List<TreeNode> delNodes(TreeNode root, int[] to_delete) {\n        ArrayList<TreeNode> list = new ArrayList<>();\n        for(int i=0;i<to_delete.length;i++){\n            if(root.val==to_delete[i]){\n                break;\n            }\n            if(i==to_delete.length-1){\n                list.add(root);\n            }\n        }\n        help(root,list,to_delete);\n        return list;\n    }\n    public TreeNode help(TreeNode root,ArrayList<TreeNode> list,int[] to_delete){\n        if(root==null){\n            return null;\n        }\n        TreeNode left = help(root.left,list,to_delete);\n        TreeNode right = help(root.right,list,to_delete);\n        if(left==null){\n            root.left=null;\n        }\n        if(right==null){\n            root.right=null;\n        }\n        for(int i=0;i<to_delete.length;i++){\n            if(root.val==to_delete[i]){\n                if(root.left!=null){\n                    list.add(root.left);\n                }\n                if(root.right!=null){\n                    list.add(root.right);\n                }\n                return null;\n            }\n        }\n        return root;\n    }\n}\n```\n```C++ []\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n    vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) {\n        vector<TreeNode*> list ;\n        for(int i=0;i<to_delete.size();i++){\n            if(root->val==to_delete[i]){\n                break;\n            }\n            if(i==to_delete.size()-1){\n                list.push_back(root);\n            }\n        }\n        help(root,list,to_delete);\n        return list;\n    }\n    TreeNode* help(TreeNode* root,vector<TreeNode*>& list,vector<int>& to_delete){\n        if(root==NULL){\n            return NULL;\n        }\n        TreeNode* left = help(root->left,list,to_delete);\n        TreeNode* right = help(root->right,list,to_delete);\n        if(left==NULL){\n            root->left=NULL;\n        }\n        if(right==NULL){\n            root->right=NULL;\n        }\n        for(int i=0;i<to_delete.size();i++){\n            if(root->val==to_delete[i]){\n                if(root->left!=NULL){\n                    list.push_back(root->left);\n                }\n                if(root->right!=NULL){\n                    list.push_back(root->right);\n                }\n                return NULL;\n            }\n        }\n        return root;\n    }\n};\n```\n\n\n\n\n### Problem Statement\nYou are given the root of a binary tree and an array `to_delete` containing values of nodes to delete. After deleting these nodes, the tree splits into multiple smaller trees (a forest). The task is to return the roots of these remaining trees.\n\n### Explanation\n\n1. **Initialization**:\n   ```java\n   ArrayList<TreeNode> list = new ArrayList<>();\n   ```\n   - An `ArrayList` is created to store the roots of the remaining trees.\n\n2. **Checking if Root Should Be Added**:\n   ```java\n   for(int i = 0; i < to_delete.length; i++){\n       if(root.val == to_delete[i]){\n           break;\n       }\n       if(i == to_delete.length - 1){\n           list.add(root);\n       }\n   }\n   ```\n   - The loop checks if the root node\'s value is in the `to_delete` array.\n   - If the root node\'s value is not in the `to_delete` array, the root is added to the `list`.\n\n3. **Recursive Function Call**:\n   ```java\n   help(root, list, to_delete);\n   ```\n   - The `help` function is called to process the tree recursively and modify it based on the `to_delete` array.\n\n4. **help Function**:\n   - **Parameters**:\n     - `TreeNode root`: The current node being processed.\n     - `ArrayList<TreeNode> list`: The list of tree roots.\n     - `int[] to_delete`: The array of values to delete.\n   - **Returns**: The modified root of the current subtree or `null` if the node is deleted.\n\n   - **Base Case**:\n     ```java\n     if(root == null){\n         return null;\n     }\n     ```\n     - If the current node is `null`, return `null`.\n\n   - **Recursive Calls**:\n     ```java\n     TreeNode left = help(root.left, list, to_delete);\n     TreeNode right = help(root.right, list, to_delete);\n     ```\n     - Recursively process the left and right children.\n\n   - **Nullify Deleted Children**:\n     ```java\n     if(left == null){\n         root.left = null;\n     }\n     if(right == null){\n         root.right = null;\n     }\n     ```\n\n   - **Check for Deletion**:\n     ```java\n     for(int i = 0; i < to_delete.length; i++){\n         if(root.val == to_delete[i]){\n             if(root.left != null){\n                 list.add(root.left);\n             }\n             if(root.right != null){\n                 list.add(root.right);\n             }\n             return null;\n         }\n     }\n     ```\n     - If the current node\'s value is in the `to_delete` array, its children (if any) are added to the `list`, and the current node is returned as `null` (deleted).\n\n5. **Return Result**:\n   ```java\n   return list;\n   ```\n\n### Example\n\nLet\'s consider an example to illustrate the working of this code:\n\n**Input**:\n- Tree: \n  ```\n      1\n     / \\\n    2   3\n   /   / \\\n  4   5   6\n  ```\n- `to_delete` = [3, 5]\n\n**Output**:\n- Forest:\n  ```\n    1     6\n   /     /\n  2     5\n /\n4\n  ```\n\n**Explanation**:\n1. Start with the root (1). It\'s not in `to_delete`, so add it to the list.\n2. Move to the left child (2), then to (4). Both are not in `to_delete`, so they remain part of the tree.\n3. Move to the right child (3). It\'s in `to_delete`, so its children (5 and 6) are considered for the new forest.\n4. Process (5). It\'s in `to_delete`, so it\'s removed. \n5. Process (6). It\'s not in `to_delete`, so it remains part of the forest.\n\nThe final forest contains the trees rooted at 1 and 6.\n```\n\n### Complexity\n1. **Time Complexity** : O(n * d), where n is the number of nodes in the tree and d is the number of nodes in to_delete.\n2. **Space Complexity** : O(n), due to the space used for recursion and the list storing the roots of the remaining trees.\n\n
['Array', 'Tree', 'Depth-First Search', 'C++', 'Java']