Skip to content

Instantly share code, notes, and snippets.

@StevenKulesza
Created June 14, 2019 01:33
Show Gist options
  • Select an option

  • Save StevenKulesza/dfd2a8e6493c9d422a32450ddfe3b5d7 to your computer and use it in GitHub Desktop.

Select an option

Save StevenKulesza/dfd2a8e6493c9d422a32450ddfe3b5d7 to your computer and use it in GitHub Desktop.
breadth first search
function bfs(tree, value) {
var queue = []
queue.push(tree[0])
while (queue.length !== 0) {
for (let i = 0; i < queue.length; i++) {
var node = queue.shift()
if (node.value === value) {
return node
}
if (node.left) {
queue.push(tree[node.left])
}
if (node.right) {
queue.push(tree[node.right])
}
}
}
return null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment