Skip to main content

Breadth-first Search

Instead of traveling depth first, the breadth first traversal traveling each level from the top of the tree to bottom. To implement this algorithm we need a queue to help us store the children node's.

Implementation

function breadthFirstTraversal(queue, array){
while(queue.length){
const node = queue.shift()

array.push(node.value)

if (node.left){
queue.push(node.left)
}

if (node.right){
queue.push(node.right)
}
}

return array
}