I’m trying to solve this question on LeetCode. I’ve managed to implement Trie using this code as reference. However, I was confused what this piece of code does:
stack = list(root.children.values())
res = ''
while stack:
node = stack.pop(-1)
if node.end:
if len(res) < len(node.word) or (len(res) == len(node.word) and res > node.word):
res = node.word
stack.extend(list(node.children.values()))
I know that it performs DFS on the Trie, but can someone help me trace the values of this code at every iteration?