trie c++
\nclass Solution {\npublic:\n    \n    struct trie\n    {\n        trie* c[26];\n        bool isend;\n        \n        trie()\n        {\n            isend=fal
```\nclass Solution {\npublic:\n    \n    struct trie\n    {\n        trie* c[26];\n        bool isend;\n        \n        trie()\n        {\n            isend=false;\n            for(int i=0;i<26;i++) c[i]=nullptr;\n        }\n    };\n    \n    trie* root= new trie();\n    string ans;\n    \n    void insert(string& word)\n    {\n        trie* temp = root;\n        for(auto& letter: word)\n        {\n            int idx = letter - \'a\';\n            \n            if(temp->c[idx] == nullptr)\n            {\n                temp->c[idx] = new trie();\n            }\n            \n            temp = temp->c[idx]; \n        }\n        \n        temp->isend = true;\n    }\n    \n    void dfs(trie* root,string temp)\n    {\n        for(int i=0;i<26;i++)\n        {\n            if(root->c[i]!=nullptr and root->c[i]->isend==true)\n            {\n                temp+=char(i+\'a\');\n                if(temp.size()>ans.size()) ans=temp;\n                else if(temp.size()==ans.size() and ans>temp)   ans=temp;\n                dfs(root->c[i],temp);\n                temp.pop_back();\n            }\n        }\n        \n    }\n    \n    string longestWord(vector<string>& words) {\n        for(auto word:words)\n        {\n            insert(word);\n        }\n        dfs(root,"");\n        return ans;\n    }\n};\n```
[]