Last active
September 26, 2022 17:13
-
-
Save alehatsman/cb0ddc99ff0bb4a8ab7f642c9dd8edce to your computer and use it in GitHub Desktop.
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
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