C++ | Passes all tests | Explanation | 95% Faster | 80% less mem
\n/*\nImportant to note:\n1) Here we use Map in Node as to unordered_map. This is due to the fact that when you will\n\trun dfs or serialize on the subfolders a
```\n/*\nImportant to note:\n1) Here we use Map in Node as to unordered_map. This is due to the fact that when you will\n\trun dfs or serialize on the subfolders althrough they have exact same folder structure. \n\tThe key in the duplicates map might be different when you use unordered_map.\n2) It is very important to define the structure of root->child i.e. by adding ( and ) as necessary\n\tas different folder structure might also lead to same keys in duplicates again causing\n\tproblems.\n*/\nstruct Node {\n    map<string, Node*> child;\n    bool toDelete = false;\n};\n\nclass Solution {\n    unordered_map<string, vector<Node*>> duplicates;\n    vector<vector<string>> ans;\npublic:\n    string dfs(Node* root) {\n        string currValue = "";\n        if (root->child.size() > 0) {\n            currValue += "(";\n        } else {\n            return "";\n        }\n        for (auto& child : root->child) {\n            currValue += child.first + dfs(child.second);\n        }\n        currValue += ")";\n        \n        if (currValue != "") {\n            duplicates[currValue].push_back(root);\n        }\n        \n        return currValue;\n    }\n    \n    void insert(Node* root, vector<string>& path, int i) {\n        if (i >= path.size()) return;\n        if (root->child.find(path[i]) == root->child.end()) {\n            root->child[path[i]] = new Node();\n        }\n        insert(root->child[path[i]], path, i + 1);\n    }\n    \n    void removeUnwantedNodes(Node* root, vector<string>& temp) {\n        if (root->toDelete) return;\n        for(auto& child : root->child) {\n            temp.push_back(child.first);\n            removeUnwantedNodes(child.second, temp);\n            temp.pop_back();\n        }\n        if (temp.size() > 0) {\n            ans.push_back(temp);\n        }\n    }\n    \n    vector<vector<string>> deleteDuplicateFolder(vector<vector<string>>& paths) {\n        Node* root = new Node();\n        for (auto& path : paths) {\n            insert(root, path, 0);\n        }\n        dfs(root);\n        \n        for (auto& dup : duplicates) {\n            if (dup.second.size() > 1) {\n                for (auto& node : dup.second) {\n                    node->toDelete = true;\n                }\n            }\n        }\n        \n        vector<string> temp;\n        removeUnwantedNodes(root, temp);\n        \n        return ans;\n    }\n};\n```
['Trie', 'C']