Skip to content

Instantly share code, notes, and snippets.

@benfluleck
Last active April 27, 2019 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benfluleck/81a8ad9a7fa28631514317ef92150395 to your computer and use it in GitHub Desktop.
Save benfluleck/81a8ad9a7fa28631514317ef92150395 to your computer and use it in GitHub Desktop.
Depth First Search
function Node(value){
this.value = value
this.left = null;
this.right = null;
}
Node.prototype.toString = function() {
return this.value;
}
Node.prototype.depthFirstSearch = function depthFirstSearch(rootNode) {
if (rootNode === null) {
return;
}
// Print the data of the node.
console.log(rootNode.value);
depthFirstSearch(rootNode.left);
depthFirstSearch(rootNode.right);
}
@benfluleck
Copy link
Author

Most DFS algorithms use a stack to implement however in our case we are using a recursion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment