Easy and Conceptual C++ Solution-O(N) Solution-(10 ms Beats 91.76%)
Intuition\n Describe your first thoughts on how to solve this problem. \n\nIntuition of solving this quesiton is easy. we just have delete given node and if tha
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nIntuition of solving this quesiton is easy. we just have delete given node and if that node has left and right child store them into result as now they have become root of another created tree(forest).\n\nthere are muliple ways of travesing tree however if we use try to traverse in top-down mannner we will get into problem.(just do dry run you will get idea). so it\'s better to approch this question by travesing into bottom-up manner(dfs). \n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nsteps:\n\n1) convet to_delete array to Set data Structure\n    -> searching in vector takes O(n) time while in unordered_set it takes O(logN).\n\n2) apply dfs and reach at last level and while doing backtracking apply logic of deleting node and try to update result.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\nfor step-1) O(N)\nfor step-2) O(logN)+O(N)\n\nso overall time complexity of this approch is O(N).\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(N)-as we are using set data structure \nO(N)-for recursion stack\n\nso overall space complexity of this approch is O(N).\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    TreeNode* delete_helper(TreeNode* root, unordered_set<int>&st,vector<TreeNode*>&result){\n        if(root==NULL){\n            return root;\n        }\n        root->left=delete_helper(root->left,st,result);\n        root->right=delete_helper(root->right,st,result);\n        if(st.find(root->val)!=st.end()){\n            if(root->left){\n                result.push_back(root->left);\n            }\n             if(root->right){\n                result.push_back(root->right);\n            }\n            return  NULL;\n        }else{\n            return root;\n        }\n    }\n    vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) {\n        vector<TreeNode*>ans;\n        int n=to_delete.size();\n        if(root==NULL || n==0){\n            return ans;\n        }\n        unordered_set<int>st;\n        for(auto &it:to_delete){\n                st.insert(it);\n        }\n        delete_helper(root,st,ans);\n        if(st.find(root->val)==st.end()){\n            ans.push_back(root);\n        }\n        return ans;\n    }\n};\n```
['Tree', 'Binary Tree', 'C++']