[C++] Using Hash tables for parents. Postorder O(N)
Explanation: \nUsing hash-tables we store link to parents, so that we can easily delete it from tree. We use "del" hash-table for quickly finding whether curren
Explanation: \nUsing hash-tables we store link to parents, so that we can easily delete it from tree. We use "del" hash-table for quickly finding whether current node should be deleted. Moreover, postorder traverse is used for deleting.\n\n```\nclass Solution {\npublic:\n    \n    unordered_map<TreeNode*, TreeNode*> parents;\n    unordered_map<int, bool> del;\n    vector<TreeNode*> ans;\n    \n    vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) {\n        \n        for(auto &i: to_delete){\n            del[i] = true;\n        }\n        parents[root] = new TreeNode(0);\n        linkParents(root);\n        deleteNodes(root);\n        if(!del[root->val])\n            ans.push_back(root);\n        return ans;\n        \n        \n    }\n    \n    void deleteNodes(TreeNode* root){\n        if(!root)\n            return;\n        deleteNodes(root->left);\n        deleteNodes(root->right);\n        if(del[root->val]){\n            if(root->left)\n                ans.push_back(root->left);\n            if(root->right)\n                ans.push_back(root->right);\n            if(root == parents[root]->left)\n                parents[root]->left = NULL;\n            else if(root == parents[root]->right)\n                parents[root]->right = NULL;\n        }\n    }\n    \n    void linkParents(TreeNode* root){\n        if(!root)\n            return;\n        if(root->left)\n            parents[root->left] = root;\n        if(root->right) \n            parents[root->right] = root;\n        linkParents(root->left);\n        linkParents(root->right);\n    }\n};\n```
[]