Help with Trie question

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

I know that it performs DFS on the Trie, but can someone help me trace the values of this code at every iteration?

Trie you are using is nested dictionary… example-: [“a”, “ap”, “app”, “apple”]
The structure when stored will be-:
{“isEnd”: 1, “a”: {“isEnd” : 1, “p” : {“isEnd”, “p” : {“l” : {“isEnd”: 1, “e”}}}}}
here “isEnd” represent the end of the word in each layer. We have to find the max depth of contigious “isEnd”, which is 3, and the biggest valid word is “app”.
A child is added on to the stack if its is valid (i.e. there is “isEnd” (end of word) in its corresponding dictionary…

Below is my solution…