Implemnting a Trie Data structures problem from HackerRank using Python3
You could run autopep8 for minor style corrections (e.g. whitespaces).
Naming variables correctly is always extremely important, especially in a dynamic language, without indications about object type:


Your trie is a Trie, not just a tree.
n sounds like an integer. It could be called lines.
i also sounds like an integer. It could be line or word.

Using for/else syntax, you can refactor your main method without any flag.




if __name__ == "__main__":
    trie = Trie()
    _header, *lines = stdin.readlines()
    for line in lines:
        word = line.strip()
        if trie.add(word):
            print("BAD SET")
            print(word)
            break
    else:
        print("GOOD SET")





If you wish, you could replace the 3 last lines of add by return fail or current.counter &gt;= 2