-
-
Save hellpanderrr/40937c337e64d4ce36bbfd351f572585 to your computer and use it in GitHub Desktop.
python traverse n-ary tree depth-first without recursion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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