I'm trying to understand recursion better in Javascript with this code for a BST. I can print out a BST's values with two methods using recursion, but I can't figure out how to do it all in one method.
how can I combine
BinarySearchTree.prototype.dfs = function(node) {
if (node) {
console.log(node.val);
this.dfs(node.left);
this.dfs(node.right);
}
}
BinarySearchTree.prototype.depthFirstTraversal = function() {
let current = this.root;
this.dfs(current);
}
into one function? I've been trying
BinarySearchTree.prototype.sameFunction = function(node = null) {
// if node is null use this.root
let current = node || this.root;
if (current) {
console.log(current.val);
this.sameFunction(current.left);
this.sameFunction(current.right);
}
}