Simple and easy || C++ || 98.33% beats
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n\n\n1. Global Variable:\n   - vector<TreeNode*> ans;: This vector will st
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n\n\n1. **Global Variable**:\n   - `vector<TreeNode*> ans;`: This vector will store the nodes that remain after deletion.\n\n2. **Function `solve`**:\n   - This recursive function is used to traverse the binary tree and delete nodes.\n   - It takes the following parameters:\n     - `root`: A reference to a pointer to the current node in the tree.\n     - `todelete`: A vector containing the values of nodes to be deleted.\n   - Inside the function:\n     - Recursively traverse the left and right subtrees.\n     - If the current node\'s value is in the `todelete` vector:\n       - Add its left child (if exists) and right child (if exists) to the `ans` vector.\n       - Set the current node to `NULL` (effectively deleting it).\n     - Note: The `delete root;` line is unnecessary because setting `root` to `NULL` already disconnects it from the tree.\n\n3. **Function `delNodes`**:\n   - This function is the entry point for deleting nodes from the binary tree.\n   - It takes the following parameters:\n     - `root`: A pointer to the root of the binary tree.\n     - `todelete`: A vector containing the values of nodes to be deleted.\n   - Inside the function:\n     - If the root\'s value is in the `todelete` vector, do nothing (since the root itself will be deleted).\n     - Otherwise, add the root to the `ans` vector.\n     - Call the `solve` function to handle the rest of the tree.\n     - Return the `ans` vector containing the remaining nodes.\n\n4. **Overall Purpose**:\n   - The code aims to delete specific nodes from the binary tree based on the values provided in the `todelete` vector.\n   - It constructs a new vector (`ans`) containing the nodes that remain after deletion.\n\n5. **Note**:\n   - The code snippet assumes that the `TreeNode` class has appropriate members for left and right children.\n   - The `find` function is used to check if a value exists in the `todelete` vector.\n\nIn summary, this code performs a post-order traversal of the binary tree, identifies nodes to be deleted, and constructs a new vector with the remaining nodes.\n\nFeel free to explore further or ask if you have any specific questions! \uD83D\uDE0A\n\n# Complexity\n- Time complexity: O(number of nodes in the tree) or O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n) (worst case due to recursion stack)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\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*>ans;\n    \n    void solve(TreeNode* &root, vector<int>& todelete){\n        \n        if(root!=NULL){\n        \n        solve(root->left,todelete);\n        solve(root->right,todelete);\n        \n        if(find(todelete.begin(),todelete.end(),root->val)!=todelete.end())\n        {\n            if(root->left)\n                ans.push_back(root->left);\n            if(root->right)\n                ans.push_back(root->right);\n            \n            root=NULL;\n            delete root;\n        }    \n        \n        }\n        \n        \n        \n    }\n    \n    \n    vector<TreeNode*> delNodes(TreeNode* root, vector<int>& todelete){\n\n        if(find(todelete.begin(),todelete.end(),root->val)!=todelete.end()){\n            \n        }\n        else {\n            ans.push_back(root);\n        }\n        \n        solve(root,todelete);\n        return ans;\n         \n    }\n};\n```
['C++']