C++ Trie Solution
\nclass Solution {\npublic:\n    /** Initialize your data structure here. */\n    struct TrieNode{\n        TrieNode* arr[26];\n        bool eow;\n    };\n    \
```\nclass Solution {\npublic:\n    /** Initialize your data structure here. */\n    struct TrieNode{\n        TrieNode* arr[26];\n        bool eow;\n    };\n    \n    /**Build a function to make a node and return TrieNode **/\n    TrieNode* getNode()\n    {\n        TrieNode* node = new TrieNode;\n        for(int i=0;i<26;i++)\n            node->arr[i]=NULL;\n        node->eow = false;\n        \n        return node;\n    }\n    \n    /** Make the root TrieNode **/\n    TrieNode* root;\n    \n    Solution(){\n        root = getNode();\n    }\n    \n    /** Insert a word into Trie **/\n    void insert(string word)\n    {\n        TrieNode* temp = root;\n        for(auto w: word)\n        {\n            int index = w - \'a\';\n            if(temp->arr[index]==NULL)\n                temp->arr[index] = getNode();\n            temp = temp->arr[index];\n            \n        }\n        temp->eow = true;\n    }\n    \n    \n    /** SPECIAL Search function specially for this problem **/\n    \n    string search()\n    {\n        string ans="";\n        int maxvalue=0;\n        spsearch(root,0,"",maxvalue,ans);\n        \n        return ans;\n    }\n    \n    void spsearch(TrieNode* r,int curr,string currstr,int& max,string& ans)\n    {\n        TrieNode* temp = r;\n        \n        //For each node check all alphabets from a-z (Since lexico) \n        for(int i=0;i<26;i++)\n        {\n            if(temp->arr[i]==NULL)\n                continue;\n            \n            if(temp->arr[i]->eow)\n            {\n                curr++;\n                currstr+=(char)(i+\'a\');\n                \n                // cout<<"--------------------"<<endl;\n                // cout<<" Word: "<<(char)(i+\'a\')<<"   "<<curr<<"  y: "<<currstr<<endl;\n                ans = curr>max ? currstr : ans;\n                max = curr>max ? curr : max;\n                \n                \n                // cout<<" Word: "<<(char)(i+\'a\')<<"   "<<max<<"  y: "<<ans<<endl;\n                // cout<<"--------------------"<<endl;\n                spsearch(temp->arr[i],curr,currstr,max,ans);\n\n                curr--;\n                currstr.pop_back();\n            }\n            \n        }//for\n    }\n    \n    /** Main Function **/\n    string longestWord(vector<string>& words) {\n        \n        for(auto word:words)\n            insert(word);\n        \n        string result = search();\n        \n        return result;\n    }\n};\n```
[]