O(n) | Beats 100% | Java  | C++ | Python | JavaScript | Go |
Java []\n\nclass Solution {\n    private boolean[] toDelete = new boolean[1001];\n    private List<TreeNode> forest = new ArrayList<>();\n\n    public List<Tree
```Java []\n\nclass Solution {\n    private boolean[] toDelete = new boolean[1001];\n    private List<TreeNode> forest = new ArrayList<>();\n\n    public List<TreeNode> delNodes(TreeNode root, int[] to_delete) {\n        for (int val : to_delete) {\n            toDelete[val] = true;\n        }\n\n        dfs(root, true);\n\n        return forest;\n    }\n\n    private TreeNode dfs(TreeNode node, boolean isRoot) {\n        if (node == null) {\n            return null;\n        }\n\n        boolean shouldDelete = toDelete[node.val];\n\n        if (isRoot && !shouldDelete) {\n            forest.add(node);\n        }\n\n        node.left = dfs(node.left, shouldDelete);\n        node.right = dfs(node.right, shouldDelete);\n\n        return shouldDelete ? null : node;\n    }\n}\n\n```\n```C++ []\nclass Solution {\npublic:\n    vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) {\n        vector<bool> toDelete(1001, false);\n        vector<TreeNode*> forest;\n        \n        for (int val : to_delete) {\n            toDelete[val] = true;\n        }\n        \n        dfs(root, true, toDelete, forest);\n        \n        return forest;\n    }\n    \nprivate:\n    TreeNode* dfs(TreeNode* node, bool isRoot, vector<bool>& toDelete, vector<TreeNode*>& forest) {\n        if (!node) return nullptr;\n        \n        bool shouldDelete = toDelete[node->val];\n        \n        if (isRoot && !shouldDelete) {\n            forest.push_back(node);\n        }\n        \n        node->left = dfs(node->left, shouldDelete, toDelete, forest);\n        node->right = dfs(node->right, shouldDelete, toDelete, forest);\n        \n        return shouldDelete ? nullptr : node;\n    }\n};\n\n\n```\n```Python []\nclass Solution:\n    def delNodes(self, root: TreeNode, to_delete: List[int]) -> List[TreeNode]:\n        to_delete_set = set(to_delete)\n        forest = []\n        \n        def dfs(node: TreeNode, is_root: bool) -> TreeNode:\n            if not node:\n                return None\n            \n            should_delete = node.val in to_delete_set\n            \n            if is_root and not should_delete:\n                forest.append(node)\n            \n            node.left = dfs(node.left, should_delete)\n            node.right = dfs(node.right, should_delete)\n            \n            return None if should_delete else node\n        \n        dfs(root, True)\n        return forest\n\n\n```\n```Go []\nfunc delNodes(root *TreeNode, to_delete []int) []*TreeNode {\n    toDelete := make([]bool, 1001)\n    forest := []*TreeNode{}\n    \n    for _, val := range to_delete {\n        toDelete[val] = true\n    }\n    \n    var dfs func(*TreeNode, bool) *TreeNode\n    dfs = func(node *TreeNode, isRoot bool) *TreeNode {\n        if node == nil {\n            return nil\n        }\n        \n        shouldDelete := toDelete[node.Val]\n        \n        if isRoot && !shouldDelete {\n            forest = append(forest, node)\n        }\n        \n        node.Left = dfs(node.Left, shouldDelete)\n        node.Right = dfs(node.Right, shouldDelete)\n        \n        if shouldDelete {\n            return nil\n        }\n        return node\n    }\n    \n    dfs(root, true)\n    return forest\n}\n\n\n```\n\n```JavaScript []\n/**\n * @param {TreeNode} root\n * @param {number[]} to_delete\n * @return {TreeNode[]}\n */\nvar delNodes = function(root, to_delete) {\n    const toDelete = new Array(1001).fill(false);\n    const forest = [];\n    \n    for (const val of to_delete) {\n        toDelete[val] = true;\n    }\n    \n    const dfs = (node, isRoot) => {\n        if (!node) return null;\n        \n        const shouldDelete = toDelete[node.val];\n        \n        if (isRoot && !shouldDelete) {\n            forest.push(node);\n        }\n        \n        node.left = dfs(node.left, shouldDelete);\n        node.right = dfs(node.right, shouldDelete);\n        \n        return shouldDelete ? null : node;\n    };\n    \n    dfs(root, true);\n    return forest;\n};\n\n\n```
['Array', 'Hash Table', 'Tree', 'Depth-First Search', 'Binary Tree', 'C++', 'Java', 'Go', 'Python3', 'JavaScript']