C++ Clean & Fast Code with Explanation | Faster than 90%
Firstly, construct a tree which denotes the file system. The value of every tree node is the corresponding folder name.\n\nThen, we compress every node\'s subtr
Firstly, construct a tree which denotes the file system. The value of every tree node is the corresponding folder name.\n\nThen, we compress every node\'s subtree architecture into a string. Note that in order to judge two identical tree nodes, the children of every node should be visited according to the lexigraphical order of their values when compressing the architecture information. Save the subtree strings in a hashmap `cnt` to judge whether their are duplicate ones.\n\nFinally we do a DFS on the tree and do not visit those nodes who share a duplicate subtree architecture to get the results.\n\n```c++\nclass Solution {\npublic:\n    vector<vector<string>> deleteDuplicateFolder(vector<vector<string>>& paths) {\n        constructTree(paths);\n        constructSubtreeString(root);\n        vector<string> path;\n        for (auto child: root->children)\n            deleteDup(child.second, path);\n        return this->res;\n    }\nprivate:\n    struct TreeNode{\n        string val, subtree = "";\n        map<string, TreeNode*> children;\n        TreeNode() {}\n        TreeNode(string val) {\n            this->val = val;\n        }\n    };\n    TreeNode *root;\n    unordered_map<string, int> cnt;\n    vector<vector<string>> res;\n    \n    inline void constructTree(vector<vector<string>>& paths) {\n        root = new TreeNode();\n        for (auto& path: paths) {\n            auto cur = root;\n            for (auto& name: path) {\n                if (!cur->children.count(name))\n                    cur->children[name] = new TreeNode(name);\n                cur = cur->children[name];\n            }\n        }\n    }\n    \n    inline void constructSubtreeString(TreeNode* cur) {\n        for (auto& child: cur->children) {\n            constructSubtreeString(child.second);\n            cur->subtree += child.first + "[" + child.second->subtree + "]";\n        }\n        cnt[cur->subtree]++;\n    }\n    \n    inline void deleteDup(TreeNode* cur, vector<string>& path) {\n        if (cur->subtree != "" && cnt[cur->subtree] > 1) return;\n        path.push_back(cur->val);\n        res.push_back(path);\n        for (auto& child: cur->children)\n            deleteDup(child.second, path);\n        path.pop_back();\n    }\n};\n```
[]