updated longestPrefix method for Trie