C++ & Python solutions, using Trie
C++ solution,\n\nclass TrieNode {\npublic:\n\tunordered_map<char,shared_ptr<TrieNode>> children;\n\tbool end = false;\n};\n\nclass Trie {\npublic:\n\tTrie () {\
C++ solution,\n```\nclass TrieNode {\npublic:\n\tunordered_map<char,shared_ptr<TrieNode>> children;\n\tbool end = false;\n};\n\nclass Trie {\npublic:\n\tTrie () {\n\t\troot = make_shared<TrieNode>();\n\t}\n\n\tvoid insert(string word) {\n\t\tshared_ptr<TrieNode> cur = root;\n\n\t\tfor (auto& c : word) {\n\t\t\tif (!cur->children.count(c))\n\t\t\t\tcur->children[c] = shared_ptr<TrieNode>(new TrieNode);\n\n\t\t\tcur = cur->children[c];\n\t\t}\n\n\t\tcur->end = true;\n\t}\n\n\tstring helper(shared_ptr<TrieNode> node, string partial_res) {\n\t\tstring res = partial_res;\n\n\t\tfor (auto& i : node->children)\n\t\t\tif (i.second->end) {\n\t\t\t\tstring res_ = helper(i.second, partial_res + i.first);\n\n\t\t\t\tif (res_.size() > res.size() || (res_.size() == res.size() && res_ < res))\n\t\t\t\t\tres = res_;\n\t\t\t}\n\n\t\treturn res;\n\t}\n\n\tstring longest_word() {\n\t\treturn helper(root, "");\n\t}\n\nprivate:\n\tshared_ptr<TrieNode> root;\n};\n\n\n//\nclass Solution {\npublic:\n\tstring longestWord(vector<string>& words) {\n\t\tTrie T;\n\n\t\tfor (auto& word : words)\n\t\t\tT.insert(word);\n\n\t\treturn T.longest_word();\n\t}\n};\n```\n\nPython solution,\n```\nclass TrieNode:\n\tdef __init__(self):\n\t\tself.children = {}\n\t\tself.end = False\n\nclass Trie:\n\tdef __init__(self):\n\t\tself.root = TrieNode()\n\n\tdef insert(self, word):\n\t\tcur = self.root\n\n\t\tfor c in word:\n\t\t\tif c not in cur.children:\n\t\t\t\tcur.children[c] = TrieNode()\n\n\t\t\tcur = cur.children[c]\n\n\t\tcur.end = True\n\n\tdef helper(self, node, partial_res):\n\t\tres = partial_res\n\n\t\tfor c, child in node.children.items():\n\t\t\tif child.end:\n\t\t\t\tpot = self.helper(child, partial_res + c)\n\t\t\t\tif len(pot) > len(res) or (len(pot) == len(res) and pot < res):\n\t\t\t\t\tres = pot\n\n\t\treturn res\n\n\tdef longest_word(self):\n\t\treturn self.helper(self.root, \'\')\n\nclass Solution:\n\tdef longestWord(self, words):\n\t\tT = Trie()\n\n\t\tfor word in words:\n\t\t\tT.insert(word)\n\n\t\treturn T.longest_word()\n```
['C', 'Python']