[JAVA 16ms (99%) @ 20180108] Trie+DFS: clean, easy, explained and illustrated
Build a trie in the normal way, then do a dfs to find the longest continuous downward path from the root. This is not a particularly hard question in the contex
Build a trie in the normal way, then do a dfs to find the longest **continuous** downward path from the root. This is not a particularly hard question in the context of trie, the point of this solution is to present a generic way of trie building and inserting that can be easily adapted to similar questions. Code:\n\n```java\nclass Solution {\n    public String longestWord(String[] words) {\n        TrieNode root = new TrieNode ();\n        root.word = "-";\n        for (String word : words)\n            root.insert (word);\n        return dfs (root, "");\n    }\n\n    String dfs (TrieNode node, String accum) {\n        if (node == null || node.word.length () == 0)\n            return accum;\n        String res = "";\n        if (!node.word.equals ("-"))\n            accum = node.word;\n        for (TrieNode child : node.links) {\n            String curRes = dfs (child, accum);\n            if (curRes.length () > res.length () || (curRes.length () == res.length () && curRes.compareTo (res) < 0))\n                res = curRes;\n        }\n        return res;\n    }\n\n    /* Hand write this class every time you need to so you can remember well */\n    static class TrieNode {\n        String word = "";\n        TrieNode[] links = new TrieNode[26];\n\n        void insert (String s) {\n            char[] chs = s.toCharArray ();\n            TrieNode curNode = this;\n            for (int i = 0; i < chs.length; i++) {\n                int index = chs[i] - 'a';\n                if (curNode.links[index] == null)\n                    curNode.links[index] = new TrieNode ();\n                curNode = curNode.links[index];\n            }\n            curNode.word = s;\n        }\n    }\n}\n```\n\nA typical trie for the list of `"ab", "ac"`:\n<img src="http://i63.tinypic.com/2jbtwjl.png" width = "300"/>
[]