MOST OPTIMAL BEGINNER FRIENDLY C++
Intuition\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 
# Intuition\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\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->image.png\n![image.png](https://assets.leetcode.com/users/images/cf39d01e-eb92-401d-868e-b56e265b4122_1713040358.2964191.png)\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*> res;\n    unordered_set<int> vis;\n    void dfs(TreeNode*& root)\n    {\n        if(!root)\n        {\n            return;\n        }\n        dfs(root->left);\n        dfs(root->right);\n        if(vis.count(root->val))\n        {\n            if(root->left)\n            {\n                res.push_back(root->left);\n            }\n            if(root->right)\n            {\n                res.push_back(root->right);\n            }\n            root = nullptr;\n        }\n    }\n    vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) {\n        ios_base::sync_with_stdio(false);\n        cin.tie(nullptr);\n        cout.tie(nullptr);\n        vis = {to_delete.begin(), to_delete.end()};\n        dfs(root);\n        if(root)\n        {\n            res.push_back(root);\n        }\n        return res;\n    }\n};\n```
['C++']