LeetCoders seem to rarely care about memory leak
As far as I observe, LeetCoders care mostly about speed and memory usage. Who wants to bother releasing heap memory?\nHowever, it does not mean one should be un
As far as I observe, LeetCoders care mostly about speed and memory usage. Who wants to bother releasing heap memory?\nHowever, it does not mean one should be unaware of it. If you use `new`, keep in mind to use `delete`. At least in a real interview, mention it.\n\nThe credit of solution itself goes to @lzl124631x.\n```\nstruct Node{\n    Node(string s = ""): name(s) {};\n    \n    string name;\n    map<string, Node*> children;\n    bool toDelete = false;\n};\n\nclass Solution {\nprivate:\n    unordered_map<string, Node*> seen;    // substructure string to the first met Node\n    vector<vector<string>> res;\npublic:\n    string mark(Node* root)\n    {\n        string subStructure = "";\n        for (auto [name, node]: root->children)\n            subStructure += mark(node);\n        \n        if (!subStructure.empty())\n        {\n            if (seen.find(subStructure) != seen.end())\n            {\n                seen[subStructure]->toDelete = true;\n                root->toDelete = true;\n            }\n            else\n                seen[subStructure] = root;\n        }\n        return "(" + root->name + subStructure + ")";\n    }\n    \n    void collect(Node* root, vector<string>& cur)\n    {\n        if (!root)\n            return;\n        for (auto [name, node]: root->children)\n        {\n            if (node->toDelete)\n                continue;\n            cur.push_back(name);\n            res.push_back(cur);\n            collect(node, cur);\n            cur.pop_back();\n        }\n    }\n    \n    void clean(Node* root)\n    {\n        if (!root)\n            return;\n        for (auto [_, node]: root->children)\n            clean(node);\n        delete root;\n    }\n    \n    vector<vector<string>> deleteDuplicateFolder(vector<vector<string>>& paths) {\n        // build the tree\n        Node* root = new Node();\n        for (auto& path: paths)\n        {\n            Node* cur = root;\n            for (auto& folder: path)\n            {\n                if (cur->children.find(folder) == cur->children.end())\n                    cur->children[folder] = new Node(folder);\n                cur = cur->children[folder];\n            }\n        }\n        \n        // mark the to-delete nodes\n        string dummy = mark(root);\n        \n        // collect qualified nodes to result\n        vector<string> cur;\n        collect(root, cur);\n        \n        // release the heap memory\n        clean(root);\n        \n        return res;\n    }\n};\n```
['C']