Trie+BFS | [Python] Solution with Comments Easy to Understand
\nclass TrieNode:\n    def __init__(self):\n        self.children = {}\n        self.endOfWord = False\n        self.val = \'\'           # to define the value 
```\nclass TrieNode:\n    def __init__(self):\n        self.children = {}\n        self.endOfWord = False\n        self.val = \'\'           # to define the value of end node as word\n\nclass Trie:\n    def __init__(self):\n        self.root = TrieNode()\n    \n    def addWord(self, word):\n        cur = self.root\n        for c in word:\n            if c not in cur.children:\n                cur.children[c] = TrieNode()\n            cur = cur.children[c]\n        cur.endOfWord = True\n        cur.val = word     # definging the end value as whole word\n    \n    def bfs(self):         # as I am starting from the root itself, so bfs have only argument self for referring the root\n        q = [self.root]\n        res = \'\'\n        while q:\n            cur = q.pop(0)\n            for child in cur.children.values():     # traversing all the nodes of cur not keys\n                if child.endOfWord:                 # ONLY go to the node which is of length 1 and end of a word also\n                    q.append(child)\n                    if len(child.val) > len(res):   # for greater length as for lexicografical I have already sorted words \n                        res = child.val\n        return res\n        \nclass Solution:\n    def longestWord(self, words: List[str]) -> str:\n        words.sort()        # sort in lexicografical order\n        trie = Trie()       # making object of the Trie Class\n        for word in words:  # adding all words to trie structre\n            trie.addWord(word)\n        \n        return trie.bfs()   # calling the bfs function \n```
['Breadth-First Search', 'Trie', 'Python', 'Python3']