Skip to content

Instantly share code, notes, and snippets.

@hellpanderrr
Created April 18, 2018 15:32
Show Gist options
  • Save hellpanderrr/40937c337e64d4ce36bbfd351f572585 to your computer and use it in GitHub Desktop.
Save hellpanderrr/40937c337e64d4ce36bbfd351f572585 to your computer and use it in GitHub Desktop.
python traverse n-ary tree depth-first without recursion
def dfs_traverse(root):
stack = []
stack.append(root)
while (stack):
node = stack.pop()
for child in node.children:
stack.append(child)
process_node(node)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment