I am trying to solve this leetcode question https://leetcode.com/problems/binary-search-tree-iterator/ where it asks you to make an iterate to traverse the BST and I thought generators are a good fit for it.
Here is my attempt
class BSTIterator {
constructor(root) {
this.root = root
this._gen = this._getGen(root)
}
*_getGen(node) {
if(node) {
yield* this._getGen(node.left)
yield node.val
yield* this._genGen(node.right)
}
}
next() {
return this._gen.next().value
}
hasNext() {
return this._gen.next().done
}
}
But I got an error saying
TypeError: yield* is not a terable
Can someone help me understand where I did wrong and what are the correct solutions to this problem by using generators?