Skip to content

Instantly share code, notes, and snippets.

@alehatsman
Last active September 26, 2022 17:13
Show Gist options
  • Save alehatsman/cb0ddc99ff0bb4a8ab7f642c9dd8edce to your computer and use it in GitHub Desktop.
Save alehatsman/cb0ddc99ff0bb4a8ab7f642c9dd8edce to your computer and use it in GitHub Desktop.
export default function bfs(head: BinaryNode<number>, needle: number): boolean {
return walk([head], needle);
}
function walk(queue: BinaryNode<number>[], needle: number): boolean {
const node = queue.shift();
if (!node) {
return false;
}
if (node.value === needle) {
return true;
}
if (node.left) {
queue.push(node.left);
}
if (node.right) {
queue.push(node.right);
}
return walk(queue, needle);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment