How We'll Begin Solving

The main challenge with traversing a BST in an inorder manner is keeping track of the ancestor. After all, at every node we visit, we will need to check if there is a node further on its left. This will need to continue until we reach a leaf node, i.e., node with no children as our starting point:

How We'll Begin Solving

Once at a leaf node, we can start visiting the other nodes as per the traversal. We now understand the point of our two methods. isThereNextNode() has to be used to see if more nodes are yet to be visited in the traversal. It will be useful to stop the traversal if we have visited all of them. As long as there are more nodes to visit, nextNode() will have to keep track of them and visit them with each call.

One relatively naive approach is to use the stack data structure. Our constructor function can initialize the stack and keep track of the ancestors as it tries to find the leaf node to start with:

How We'll Begin Solving

Then, we pop the stack and visit the node we get. We can check if the current node has a node to the right. If there is, we traverse it in an inorder way.

Have a look at the attached code that implements this approach.

JAVASCRIPT
OUTPUT
Results will appear here.