Simple logic || Two cases, check every node || 99%
Intuition\n\nSuppose the current node, say node, you\'re checking is not present in the todelete array. You leave node untouched and check it\'s left and right 
# Intuition\n\nSuppose the current node, say $$node$$, you\'re checking is not present in the $$to$$_$$delete$$ array. You leave $$node$$ untouched and check it\'s $$left$$ and $$right$$ children.\n\nIf $$node$$ is present in $$to$$_$$delete$$ array, we need to tell it\'s parent to not point to this node. We also need to check $$node\'s$$ $$left$$ and $$right$$ children and add the ones not present in the delete array to our $$answer$$ list.\n\n# Approach\nFor the case when $$node$$ is not present in $$to$$_$$delete$$ array:\n- return ```false```, signalling $$node\'s$$ parent to keep pointing to it\n- Check whether $$left$$ and $$right$$ children of $$node$$ are to be deleted or not\n\nFor the case when $$node$$ is present in $$to$$_$$delete$$ array:\n- return ```true```, signalling the parent to delete $$node$$.\n- Add $$node\'s$$ left and right children to the list, if they themselves don\'t have to be deleted.\n\nApply the above logic to the root node. If root is present in the $$to$$_$$delete$$ array, you do not include it in the $$answer$$ list, otherwise you add it. \nSame logic is repeated for every node. \n\n# Complexity\n- Time complexity:\n $$O(n)$$\nwhere $$n$$ is the number of nodes in the tree. \n\n- Space complexity:\n$$O(h + n)$$\nwhere $$h$$ is the height of the tree,$$n$$ is the number of nodes in the tree. \n\n# Code\n```Java []\nclass Solution {\n    public List<TreeNode> delNodes(TreeNode root, int[] to_delete) {\n        HashSet<Integer> set = new HashSet<>(); \n        for (int val : to_delete) {\n            set.add(val);\n        }\n        List<TreeNode> ans = new ArrayList<>();\n        if (delete(root, ans, set)) {\n            // root was deleted\n        } else {\n            ans.add(root);\n        }\n        return ans; \n    }\n    \n    // Returns true if the node was deleted\n    public boolean delete(TreeNode node, List<TreeNode> list, HashSet<Integer> set) {\n        if (node == null) {\n            return true;\n        }\n        if (set.contains(node.val)) {\n            // Node to be deleted\n            if (!delete(node.left, list, set)) {\n                list.add(node.left);\n            }\n            if (!delete(node.right, list, set)) {\n                list.add(node.right);\n            }\n            return true;\n        } else {\n            if (delete(node.left, list, set)) {\n                node.left = null;\n            }\n            if (delete(node.right, list, set)) {\n                node.right = null;\n            }\n            return false;\n        }\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) {\n        unordered_set<int> delete_set(to_delete.begin(), to_delete.end());\n        vector<TreeNode*> ans;\n        deleteNodes(root, true, delete_set, ans);\n        return ans;\n    }\n    \nprivate:\n    void deleteNodes(TreeNode* node, bool is_root, unordered_set<int>& delete_set, vector<TreeNode*>& ans) {\n        if (!node) {\n            return;\n        }\n        \n        bool is_deleted = delete_set.find(node->val) != delete_set.end();\n        if (is_root && !is_deleted) {\n            ans.push_back(node);\n        }\n        \n        deleteNodes(node->left, is_deleted, delete_set, ans);\n        deleteNodes(node->right, is_deleted, delete_set, ans);\n        \n        if (is_deleted) {\n            if (node->left) {\n                ans.push_back(node->left);\n            }\n            if (node->right) {\n                ans.push_back(node->right);\n            }\n        } else {\n            node->left = (node->left && delete_set.find(node->left->val) != delete_set.end()) ? NULL : node->left;\n            node->right = (node->right && delete_set.find(node->right->val) != delete_set.end()) ? NULL : node->right;\n        }\n    }\n};\n\n```\n```python3 []\nclass Solution:\n    def delNodes(self, root: TreeNode, to_delete: List[int]) -> List[TreeNode]:\n        def delete(node):\n            if not node:\n                return False\n            left_deleted = delete(node.left)\n            right_deleted = delete(node.right)\n            if node.val in to_delete:\n                if not left_deleted and node.left:\n                    ans.append(node.left)\n                if not right_deleted and node.right:\n                    ans.append(node.right)\n                return True\n            if left_deleted:\n                node.left = None\n            if right_deleted:\n                node.right = None\n            return False\n        \n        to_delete = set(to_delete)\n        ans = []\n        if not delete(root):\n            ans.append(root)\n        return ans\n\n```\n
['C++', 'Java', 'Python3']