Skip to content

Instantly share code, notes, and snippets.

@russellf9
Forked from GAierken/InOrderDFS.js
Created July 14, 2021 16:00
Show Gist options
  • Save russellf9/d0e6d474fb795c1b82ed6351210e7a4e to your computer and use it in GitHub Desktop.
Save russellf9/d0e6d474fb795c1b82ed6351210e7a4e to your computer and use it in GitHub Desktop.
inOrderDFS(){
// a variable to store the visited nodes
let result = []
// helper function -- accepts a node
function traverse(node){
// if node has left, recursion to find the leftest leaf node
if(node.left) traverse(node.left)
// push the node to the result
result.push(node)
// if node has right, recurstion to find the rightest leaf node
if(node.right) traverse(node.right)
}
// invoke the helper function with the root
traverse(root)
// return the final result
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment