Solution
C++ []\nstruct TrieNode {\npublic:\n    TrieNode() :  is_end{false} {\n        for (int i = 0; i < 26; i++) children[i] = nullptr;\n    }  \npublic:\n    TrieNo
```C++ []\nstruct TrieNode {\npublic:\n    TrieNode() :  is_end{false} {\n        for (int i = 0; i < 26; i++) children[i] = nullptr;\n    }  \npublic:\n    TrieNode* children[26];\n    bool is_end;\n};\nclass Trie {\npublic:\n    Trie() : root_{new TrieNode()} {}\n    void insert(const std::string& str) {\n        TrieNode* p = root_;\n        for (const auto& c : str) {\n            if (p->children[c - \'a\'] == nullptr) {\n                p->children[c - \'a\'] = new TrieNode();\n            }\n            p = p->children[c - \'a\'];\n        }\n        p->is_end = true;\n    }\n    TrieNode* getRoot() {\n        return root_;\n    }\nprivate:\n    TrieNode* root_;\n};\nclass Solution {\npublic:\n    string longestWord(vector<string>& words) {\n        auto trie = std::make_unique<Trie>();\n        for (const auto& word : words) {\n            trie->insert(word);\n        }\n        TrieNode* root = trie->getRoot();\n        std::string curr_str;\n        std::string res;\n        dfs(root, curr_str, res);\n        return res;\n    }\n    void dfs(TrieNode* curr_node, std::string& curr_str, std::string& res) {\n        if (!curr_node) return;\n        if (curr_str.length() > res.length()) res = curr_str;\n        for (int i = 0; i < 26; i++) {\n            if (curr_node->children[i] && curr_node->children[i]->is_end) {\n                curr_str.push_back(static_cast<char>(\'a\' + i)); \n                dfs(curr_node->children[i], curr_str, res);\n                curr_str.pop_back();\n            }\n        }\n    }\n};\n```\n\n```Python3 []\nclass Solution:\n    def longestWord(self, words: List[str]) -> str:\n        words.sort(key=len)\n        buildable = set()\n\n        for w in words:\n            pre = w[:-1]\n            if not pre or pre in buildable:\n                buildable.add(w)\n\n        res = \'\'\n        for w in buildable:\n            if len(w) > len(res) or (len(w) == len(res) and w < res):\n                res = w\n        return res\n```\n\n```Java []\nclass Solution {\n    String result = "";\n    public String longestWord(String[] words) {\n        TrieNode root = new TrieNode();\n        for (String word: words) {\n            buildTrie(word, root);\n        }\n        dfs(root);\n        return result;\n    }\n    private void dfs(TrieNode node) {\n        if (node == null) return;\n        if (node.word != null) {\n            result = node.word.length() > result.length() ? node.word : result;\n        }\n        for (TrieNode child: node.children) {\n            if (child != null && child.word != null)\n                dfs(child);\n        }   \n    }\n    private void buildTrie(String word, TrieNode node) {\n        for (int i = 0; i < word.length(); i++) {\n            char c = word.charAt(i);\n            if (node.children[c - \'a\'] == null) {\n                node.children[c - \'a\'] = new TrieNode();\n            }\n            node = node.children[c - \'a\'];\n        }\n        node.word = word;\n    }\n    class TrieNode {\n        TrieNode[] children;\n        String word;\n        TrieNode() {\n            children = new TrieNode[26];\n        }\n    }\n}\n```\n
['C++', 'Java', 'Python3']