Last active
January 14, 2019 19:56
-
-
Save jharris-code/e57704ce936a30b2fd2da16989adcffd to your computer and use it in GitHub Desktop.
Breadth First Search
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
//Assumes: | |
//a Queue data structure is available with push(), pop(), isEmpty() methods. | |
//each node in the Queue have left and right properties. | |
//Time Complexity: O(N) | |
//Space Complexity: O(N) | |
const bfs = (root, value) => { | |
let q = new Queue(); | |
q.push(root); | |
while(!q.isEmpty()) { | |
let n = q.pop(); | |
if(n.value === value){ | |
return n; | |
} else { | |
if(n.left) { | |
q.push(n.left); | |
} | |
if(n.right) { | |
q.push(n.right); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment