The problem was not with the recursion. Your function did call itself recursively, it just didn’t yield values outside. When you call helper(), you get an iterator as a return value, but you wanted the iterated values of that iterator to be yielded. If you want to yield recursively, you need to yield *
. Try like this:
* inOrderTraversal() {
function* helper(node) {
if (node.left !== null) {
// this line is executed, but helper is not being called
yield * helper(node.left);
}
yield node.value;
if (node.right !== null) {
yield * helper(node.right);
}
}
for (let i of helper(this.root)) {
yield i;
}
}
And while you’re at it, you can replace the for
loop with:
yield * helper(this.root)