Created
October 21, 2014 18:37
-
-
Save eldilibra/c2eb54bc2415095ea7bd to your computer and use it in GitHub Desktop.
This file contains 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
function iterativeTreeTraversal (rootNode) { | |
var visited = [rootNode]; //arrays act like stacks in js if you only use push and pop | |
var currentNode; | |
while (visited.length) { | |
currentNode = visited.pop(); | |
currentNode.children.forEach(function (child) { | |
visited.push(child); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment