C++ || 2 different solutions || recursive (post order) and BFS
Solution 1: recursive post order traversal\n\nFor faster access we turn the vector<int> for the nodes we should delete into a bitset<> since the range is quite 
### Solution 1: recursive post order traversal\n\nFor faster access we turn the ```vector<int>``` for the nodes we should delete into a ```bitset<>``` since the range is quite limited. If there range of values for the nodes was larger we would use a ```unordered_set<int>``` for lookups. I didn\'t attempt to write an interative post order traversal version.\n\n```\n    vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete_vector) {\n        bitset<1024> to_delete;\n        for (int v : to_delete_vector) to_delete[v] = true;\n\n        vector<TreeNode*> ans;\n        if (delNodes(root, to_delete, ans)) {\n            // |root| node did survive, add it the to answer.\n            ans.push_back(root);\n        }\n        return ans;\n    }\n    \n    TreeNode* delNodes(TreeNode* node, const bitset<1024>& to_delete, vector<TreeNode*>& ans) {\n        if (!node) return nullptr;\n        \n        // Visit children first (post order).\n        node->left = delNodes(node->left, to_delete, ans);\n        node->right = delNodes(node->right, to_delete, ans);\n        \n        // We need to keep this node.\n        if (!to_delete[node->val]) return node;\n\n        // Delete this node. If it has children add them to the answer.\n        if (node->left) ans.push_back(node->left);\n        if (node->right) ans.push_back(node->right);\n\n        delete node;\n        return nullptr;\n    }\n```\n\n### Solution 2: BFS\n\nThis solution is inspired by @ritiksharma8427 version from: https://leetcode.com/problems/delete-nodes-and-return-forest/discuss/1170157/Approach-%3A-Simple-BFS-or-C%2B%2B-or-Queue\n\n```\n    vector<TreeNode*> delNodes(TreeNode* root, const vector<int>& to_delete_vec) {\n        if (!root) return {};\n        \n        bitset<1024> to_delete;\n        for (int v : to_delete_vec) to_delete[v] = true;\n\n        vector<TreeNode*> ans;\n        queue<TreeNode*> q;\n        \n        if (!to_delete[root->val]) {\n            ans.push_back(root);\n        }\n        q.push(root);\n        \n        while (!empty(q)) {\n            TreeNode* node = q.front(); q.pop();\n\n            auto process = [&](TreeNode*& nodeptr) {\n                if (nodeptr) {\n                    q.push(nodeptr);\n\n                    if (to_delete[nodeptr->val]) {\n                        nodeptr = nullptr;\n                    } else if (to_delete[node->val]) {\n                        ans.push_back(nodeptr);\n                    }\n                }\n            };\n            \n            process(node->left);\n            process(node->right);\n            \n            if (to_delete[node->val]) {\n                delete node;\n            }\n        }\n        \n        return ans;\n    }\n```\n\n_As always: Feedback, comments, and questions are welcome, and leave a "like" aka (upvote) if you like this solution, subscribe to the channel and ring the bell. :)_\n
['Breadth-First Search', 'Recursion', 'C', 'C++']