💢☠💫Easiest👾Faster✅💯 Lesser🧠 🎯 C++✅Python3🐍✅Java✅C✅Python🐍✅C#✅💥🔥💫Explained☠💥🔥 Beats 💯
Intuition\n\n\nC++ []\n\n// Start Of Anuj Bhati aka Edward Scott310\'s Template...\n#pragma GCC optimize("O2")\n#include <bits/stdc++.h>\n#include <ext/pb_ds/as
# Intuition\n![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/3e499258-5426-47f6-935a-c6b4a902d432_1721195685.561033.jpeg)\n![Screenshot 2024-07-17 112457.png](https://assets.leetcode.com/users/images/3cb6e547-5a0e-46a3-ae51-5718ae84df96_1721195709.9429126.png)\n```C++ []\n\n// Start Of Anuj Bhati aka Edward Scott310\'s Template...\n#pragma GCC optimize("O2")\n#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace std;\n#define umsi unordered_map<string, int>\n// end of Anuj Bhati\'s aka Edward Scott310\'s Template\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),\n * right(right) {}\n * };\n */\nclass Solution {\nprivate:\n    vector<TreeNode*> res;\n    TreeNode* postorder(TreeNode* root, bool is_true, usi& del) {\n        if (!root)\n            return nullptr;\n        bool is_delete = false;\n        if (del.find(root->val) != del.end())\n            is_delete = true;\n        if (is_true and !is_delete)\n            res.pb(root);\n        root->left = postorder(root->left, is_delete, del);\n        root->right = postorder(root->right, is_delete, del);\n        return is_delete ? nullptr : root;\n    }\n\npublic:\n    vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) {\n        usi del(ALL(to_delete));\n        postorder(root, true, del);\n        return res;\n    }\n};\n```\n```python3 []\n# Definition for a binary tree node.\n# class TreeNode:\n#     def __init__(self, val=0, left=None, right=None):\n#         self.val = val\n#         self.left = left\n#         self.right = right\nclass Solution:\n    def delNodes(self, root: Optional[TreeNode], to_delete: List[int]) -> List[TreeNode]:\n        to_delete = set(to_delete)\n        res = []\n        def postorder(root, is_true):\n            if not root:\n                return None\n            is_delete = root.val in to_delete\n            if is_true and not is_delete:\n                res.append(root)\n            root.left = postorder(root.left, is_delete)\n            root.right = postorder(root.right, is_delete)\n            return None if is_delete else root\n\n        postorder(root, True)\n        return res\n```\n```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        Set<Integer> valsToDelete = new HashSet<>();\n        List<TreeNode> result = new ArrayList<>();\n\n        for (int number : to_delete)\n            valsToDelete.add(number);\n\n        if (!valsToDelete.contains(root.val))\n            result.add(root);\n        shouldDelete(result, valsToDelete, root);\n        return result;\n    }\n    public TreeNode shouldDelete(List<TreeNode> result, Set<Integer> valsToDelete, TreeNode root) {\n        if (root == null)\n            return null;\n\n        root.left = shouldDelete(result, valsToDelete, root.left);\n        root.right = shouldDelete(result, valsToDelete, root.right);\n\n        if (valsToDelete.contains(root.val)) {\n            if (root.left != null) \n                result.add(root.left);\n            if (root.right != null)\n                result.add(root.right);\n            return null;\n        }\n\n        return root;\n    }\n}\n```\n```C# []\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     public int val;\n *     public TreeNode left;\n *     public TreeNode right;\n *     public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {\n *         this.val = val;\n *         this.left = left;\n *         this.right = right;\n *     }\n * }\n */\npublic class Solution{\n    public IList<TreeNode> DelNodes(TreeNode root, int[] to_delete){\n        var toDelete = to_delete.ToHashSet();\n        var result = new List<TreeNode>();\n        DelNodesDfs(ref root, true);\n        return result;\n\n        void DelNodesDfs(ref TreeNode node, bool isRoot){\n            if (node != null){\n                var deleted = toDelete.Contains(node.val);\n                DelNodesDfs(ref node.left, deleted);\n                DelNodesDfs(ref node.right, deleted);\n\n                if (deleted){\n                    node = null;                \n                }\n                else if (isRoot){\n                    result.Add(node);\n                }\n            }\n        }\n    }\n}\n```\n```python []\n# Definition for a binary tree node.\n# class TreeNode(object):\n#     def __init__(self, val=0, left=None, right=None):\n#         self.val = val\n#         self.left = left\n#         self.right = right\nclass Solution(object):\n    def delNodes(self, root, to_delete):\n        """\n        :type root: TreeNode\n        :type to_delete: List[int]\n        :rtype: List[TreeNode]\n        """\n        to_delete = set(to_delete)\n        res = []\n        def postorder(root, is_true):\n            if not root:\n                return None\n            is_delete = root.val in to_delete\n            if is_true and not is_delete:\n                res.append(root)\n            root.left = postorder(root.left, is_delete)\n            root.right = postorder(root.right, is_delete)\n            return None if is_delete else root\n\n        postorder(root, True)\n        return res\n```\n```C []\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     struct TreeNode *left;\n *     struct TreeNode *right;\n * };\n */\n/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint idx;\nstruct TreeNode** ret;\nstruct TreeNode* mark(struct TreeNode* root, bool* hash, bool is_root) {\n    if (root == NULL)\n        return NULL;\n    if (is_root && !hash[root->val])\n        ret[idx++] = root;\n    root->left = mark(root->left, hash, hash[root->val]);\n    root->right = mark(root->right, hash, hash[root->val]);\n    return (hash[root->val]) ? NULL : (root);\n}\n\nstruct TreeNode** delNodes(struct TreeNode* root, int* to_delete, int to_deleteSize, int* returnSize) {\n    bool* hash = calloc(1001, sizeof(bool));\n    for (int i = 0; i < to_deleteSize; i++)\n        hash[to_delete[i]] = true;\n    ret = calloc(1000, sizeof(struct TreeNode*));\n    idx = 0;\n    mark(root, hash, true);\n    *returnSize = idx;\n    return ret;\n}\n```\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n      O(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n      O(n)\n# Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     struct TreeNode *left;\n *     struct TreeNode *right;\n * };\n */\n/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint idx;\nstruct TreeNode** ret;\nstruct TreeNode* mark(struct TreeNode* root, bool* hash, bool is_root) {\n    if (root == NULL)\n        return NULL;\n    if (is_root && !hash[root->val])\n        ret[idx++] = root;\n    root->left = mark(root->left, hash, hash[root->val]);\n    root->right = mark(root->right, hash, hash[root->val]);\n    return (hash[root->val]) ? NULL : (root);\n}\n\nstruct TreeNode** delNodes(struct TreeNode* root, int* to_delete, int to_deleteSize, int* returnSize) {\n    bool* hash = calloc(1001, sizeof(bool));\n    for (int i = 0; i < to_deleteSize; i++)\n        hash[to_delete[i]] = true;\n    ret = calloc(1000, sizeof(struct TreeNode*));\n    idx = 0;\n    mark(root, hash, true);\n    *returnSize = idx;\n    return ret;\n}\n```\n![th.jpeg](https://assets.leetcode.com/users/images/b2928ccf-c014-46de-8838-a0b2eb92c56c_1721195904.917912.jpeg)\n\n
['Array', 'Hash Table', 'Tree', 'Depth-First Search', 'C', 'Python', 'C++', 'Java', 'Python3', 'C#']