Good evening! Here's our prompt for today.
We know the binary search tree (BST) structure and the three ways to traverse it: inorder, preorder, and postorder. Now, let's create a class that takes a BST and can traverse it in an inorder fashion.
Prompt
Implement the BSTInorderTraversal
class, whose each object acts as an iterator over the inorder traversal of a BST. The class will have a constructor and two methods to traverse a BST:
BSTInorderTraversal (Node rootNode)
initializes a class object. It takes aNode
object calledrootNode
to traverse on. The iterating pointer should be initialized to some number lower than any element in the tree.int nextNode()
moves to the next node in the traversal and returns the number at the node.boolean isThereNextNode()
checks if there is a next node in the traversal to the right and returnsTrue
if there is andFalse
if not.
Note how initializing the traversal pointer to a number lower than any element in the tree would ensure the first call to the
nextNode()
function returns the smallest element in the BST.
Once the class is created, you should be able to pass a BST and call the methods as the iterator would as it traverses.
Input:
[[BSTInorderTraversal, ], 'nextNode', 'isThereNextNode', 'nextNode', 'nextNode', 'isThereNextNode', 'nextNode', 'nextNode', 'isThereNextNode'] //array of function calls
[[5,3,8,null,null,7,15],[],[],[],[],[],[],[],[]] //array of parameters
The first array represents all the function calls that will be made. The second array shows the parameters passed to each function in the respective order. See the Explanation section below.
Output:
[null,3,5,7,8,15] // array of returned values
This array represents the values that will get returned in a particular order.
Explanation:
The object will be instantiated and the methods called in the following way:
1Node root = Node(5);
2root.left = new Node(3);
3root.right = new Node(8);
4root.right->left = new Node(7);
5root.right->right = new Node(15);
6
7BSTInorderTraversal traversalObj = new BSTInorderTraversal(root]);
8traversalObj.nextNode(); //should return 3
9traversalObj.isThereNextNode(); //should return true
10traversalObj.nextNode(); //should return 5
11traversalObj.nextNode(); //should return 7
12traversalObj.isThereNextNode(); //should return true
13traversalObj.nextNode(); //should return 8
14traversalObj.nextNode(); //should return 15
15traversalObj.isThereNextNode(); //should return false
Constraints
- The number of nodes in the BST that would get traversed is in the range [1,105].
0 <= Node.val <= 106
- The
isThereNextNode
, andnextNode
will get called at most105
times.
Before moving on to the solution, try out solving the problem yourself below. The window shows how the Node
class is defined.
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
"PASSED: first call to traversalObj.nextNode() should get us 3 & first call to traversalObj.isThereNextNode() should get us True"
// Definition for a binary tree node.
// function TreeNode(val, leftNode, rightNode) {
// this.val = (val===undefined ? 0 : val)
// this.leftNode = (leftNode===undefined ? null : leftNode)
// this.rightNode = (leftNode===undefined ? null : rightNode)
// }
// /**
// * @param {Node} rootNode
// */
​
​
​
var BSTInorderTraversal = function(rootNode) {
// write code here
};
​
/**
* @return {number}
*/
BSTInorderTraversal.prototype.nextNode = function() {
// write code here
};
​
/**
* @return {boolean}
*/
BSTInorderTraversal.prototype.isThereNextNode = function() {
// write code here
Here's our guided, illustrated walk-through.
How do I use this guide?
Are you sure you're getting this? Click the correct answer from the options.
Let's Go Through The Solution
Before we move on to the problem's solution, let's check your prior knowledge and intuition.
Question 1: In what order does inorder traversal visit a tree's nodes?
Option 1: [left node, right node, root]
Option 2: [root, left node, right node]
Option 3: [left node, root, right node]
Option 4: [right node, left node, root]
Click the option that best answers the question.
- Option 1
- Option 2
- Option 3
- Option 4
On Interviewer's Mindset
Tree traversal challenges are common in tech interviews, and BSTs are a particular favorite of interviewers. The special policy of BST makes related problems come with an inherent constraint. Combined with other constraints of a problem, BST-based problems can show how well an applicant understands trees and the efficient way of traversing them.
With a question like this, your programming skills are being tested. Since you have to define a class and its methods, you can use object-oriented programming to your advantage. Hence, make sure to understand this question and its solution thoroughly.
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:

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:

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.
xxxxxxxxxx
console.log('traversalObj.isThereNextNode()', traversalObj.isThereNextNode());
// Definition for a binary tree node.
function TreeNode(val, leftNode, rightNode) {
this.val = (val===undefined ? 0 : val)
this.leftNode = (leftNode===undefined ? null : leftNode)
this.rightNode = (leftNode===undefined ? null : rightNode)
}
​
class Stack {
​
constructor()
{
this.items = [];
}
​
push(element)
{
this.items.push(element);
}
​
pop()
{
if (this.items.length == 0)
return;
return this.items.pop();
}
​
}
​
​
Is There A Better Solution?
While this solution was pretty straightforward, there is much room for improvement. The complexity of the above solution is O(N)
, where N
is the number of nodes in the tree. Since it's a traversal challenge and we have to visit each node, this makes sense.
However, the space complexity here is also O(N)
since the stack will eventually hold all nodes. If the tree has a long height, the stack could manage thousands of nodes simultaneously. Hence, we can try to improve this complexity.
A better approach (called Morris Traveral) uses recursion to solve the problem efficiently. It also uses the fact that we can save references to nodes in the Node
object's attributes: Node rightNode
and Node leftNode
as shown above.
When we start from a certain node A
, we try to find the rightmost leaf node of its left and right subtrees. Once we reach this leaf node, we see its right node stores null
. We can use this pointer to store the address of the ancestor node A
:

Are you sure you're getting this? Click the correct answer from the options.
Question 2
Given the following recursive function, will it iterate infinitely if we call recur(5)
?
1def recur(num):
2 if num == 1:
3 return num
4 else:
5 return recur(num-1)+recur(num-3)
Click the option that best answers the question.
- True
- False
This approach uses temporary objects and pointers that we use to hold references. These variables' values constantly get reassigned. Hence, the space complexity becomes constant!.
If you haven't by now, try solving the problem before viewing the final solution.
One Pager Cheat Sheet
- Create a
BSTInorderTraversal
class that implements an iterator that traverses a binary search tree (BST) in an inorder fashion. - Inorder traversal visits the nodes of a tree in the order
left node, root, right node
in arecursive
manner. - Candidates should
understand
the BST-based problem and use object-oriented programming to their advantage when solving it. - We can use a naive
stack
approach to traverse a binary search tree (BST) in an inorder manner, starting from a leaf node and visiting each ancestor while checking if there is a node to the right. - We can improve the efficiency of the traversal challenge by using the
Morris Traversal
approach, which usesrecursion
and stores references to nodes in theNode
objects's attributes, to avoids using extra memory and reduce time complexity. - The function
recur
will infinitely recur since each time it is called it subtracts 3 from its argument so thebase case
ofnum==1
is never reached. - The
space complexity
of this approach is constant as values of variables are constantly reassigned.
This is our final solution.
To visualize the solution and step through the below code, click Visualize the Solution on the right-side menu or the VISUALIZE button in Interactive Mode.
xxxxxxxxxx
}
// Definition for a binary tree node.
function TreeNode(val, leftNode, rightNode) {
this.val = (val===undefined ? 0 : val)
this.leftNode = (leftNode===undefined ? null : leftNode)
this.rightNode = (leftNode===undefined ? null : rightNode)
}
​
/**
* @param {TreeNode} rootNode
*/
​
var BSTInorderTraversal = function(rootNode) {
this.currentNode = rootNode;
this.rightMostNode = null;
};
​
/**
* @return {number}
*/
BSTInorderTraversal.prototype.nextNode = function() {
if (!this.isThereNextNode())
return;
​
if (this.currentNode.leftNode == null) {
let temp = this.currentNode;
this.currentNode = this.currentNode.rightNode;
return temp.val;
}
​
this.rightMostNode = this.currentNode.leftNode;
​
while (this.rightMostNode.rightNode != null
That's all we've got! Let's move on to the next tutorial.
If you had any problems with this tutorial, check out the main forum thread here.