Binary Tree Iterator for Inorder Traversal
Last Updated :
23 Jul, 2025
Given a Binary Tree and an input array. The task is to create an Iterator that utilizes next() and hasNext() functions to perform Inorder traversal on the binary tree.
Examples:
Input: 8 Input Array = [next(), hasNext(), next(), next(), next(), hasNext(), next(), next(), hasNext()]
/ \
3 9
/ \
2 4
\
5
Output: [2, true, 3, 4, 5, true, 8, 9, false]
Explanation: According to in order traversal answer to the input array is calculated.
Inorder traversal = {2, 3, 4, 5, 8, 9}
Input: 4 Input Array = [hasNext(), next(), next(), hasNext()]
/ \
3 2
\
1
Output: [true, 3, 4 true]
Naive Approach: A way is needed to traverse back to the ancestor once we reach the leaf node of the binary tree. A Stack data structure can be used for this.
Algorithm:
Class is Instantiated
- initialize the stack
- set current node = root
- while current != NULL
- add current to stack
- current = current.left
hasNext() function
IF the stack is not empty
return true
ELSE
return false
next() function
- IF stack is empty (or hasNext() returns false)
- ELSE
- Initialize current = stack.top
- Pop the element from the stack
- If current.right != NULL
- Initialize next = current->right
- while next != NULL
- add next to the stack
- next = next.left
- return current
Below is the implementation of above approach
C++
// CPP Program for above approach
#include <iostream>
#include <stack>
using namespace std;
// Structure of a Node
struct Node {
int data;
Node* left;
Node* right;
};
// Utility function to create a new Node
Node* newNode(int data)
{
Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return node;
}
// Inorder Iterator class
class InorderIterator {
private:
stack<Node*> traversal;
public:
InorderIterator(Node* root)
{
moveLeft(root);
}
void moveLeft(Node* current)
{
while (current) {
traversal.push(current);
current = current->left;
}
}
bool hasNext()
{
return !traversal.empty();
}
Node* next()
{
if (!hasNext())
throw "No such element Exists";
Node* current = traversal.top();
traversal.pop();
if (current->right)
moveLeft(current->right);
return current;
}
};
// Driver Code
int main()
{
Node* root = newNode(8);
root->right = newNode(9);
root->left = newNode(3);
root->left->left = newNode(2);
root->left->right = newNode(4);
root->left->right->right = newNode(5);
InorderIterator itr(root);
try {
cout << itr.next()->data << " ";
cout << itr.hasNext() << " ";
cout << itr.next()->data << " ";
cout << itr.next()->data << " ";
cout << itr.next()->data << " ";
cout << itr.hasNext() << " ";
cout << itr.next()->data << " ";
cout << itr.next()->data << " ";
cout << itr.hasNext() << " ";
}
catch (const char* msg) {
cout << msg;
}
return 0;
}
// This code is contributed by adityamaharshi21
Java
// Java Program for above approach
import java.util.*;
// Structure of a Node
class Node {
int data;
Node left;
Node right;
Node(int data)
{
this.data = data;
left = right = null;
}
}
// Inorder Iterator class
class InorderIterator {
private Stack<Node> traversal;
InorderIterator(Node root)
{
traversal = new Stack<Node>();
moveLeft(root);
}
private void moveLeft(Node current)
{
while (current != null) {
traversal.push(current);
current = current.left;
}
}
public boolean hasNext()
{
return !traversal.isEmpty();
}
public Node next()
{
if (!hasNext())
throw new NoSuchElementException();
Node current = traversal.pop();
if (current.right != null)
moveLeft(current.right);
return current;
}
}
// Class to Test given set of inputs
class Test {
// Driver Code
public static void main(String args[])
{
Node root = new Node(8);
root.right = new Node(9);
root.left = new Node(3);
root.left.left = new Node(2);
root.left.right = new Node(4);
root.left.right.right = new Node(5);
InorderIterator itr = new InorderIterator(root);
try {
System.out.print(itr.next().data + " ");
System.out.print(itr.hasNext() + " ");
System.out.print(itr.next().data + " ");
System.out.print(itr.next().data + " ");
System.out.print(itr.next().data + " ");
System.out.print(itr.hasNext() + " ");
System.out.print(itr.next().data + " ");
System.out.print(itr.next().data + " ");
System.out.print(itr.hasNext() + " ");
}
catch (NoSuchElementException e) {
System.out.print("No such element Exists");
}
}
}
Python
# Python Program for above approach
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# Inorder Iterator class
class InorderIterator:
def __init__(self, root):
self.traversal = []
self.moveLeft(root)
def moveLeft(self, current):
while current != None:
self.traversal.append(current)
current = current.left
def hasNext(self):
return len(self.traversal) > 0
def next(self):
if not self.hasNext():
raise Exception('No such element Exists')
current = self.traversal.pop()
if current.right != None:
self.moveLeft(current.right)
return current
# Class to Test given set of inputs
# Driver Code
root = Node(8)
root.right = Node(9)
root.left = Node(3)
root.left.left = Node(2)
root.left.right = Node(4)
root.left.right.right = Node(5)
itr = InorderIterator(root)
try:
print(itr.next().data)
print(itr.hasNext())
print(itr.next().data)
print(itr.next().data)
print(itr.next().data)
print(itr.hasNext())
print(itr.next().data)
print(itr.next().data)
print(itr.hasNext())
except Exception as e:
print("No such element Exists")
# This code is contributed by adityamaharshi21
C#
// C# code for the above approach
using System;
using System.Collections.Generic;
// Structure of a Node
class Node
{
public int Data { get; set; }
public Node Left { get; set; }
public Node Right { get; set; }
public Node(int data)
{
Data = data;
Left = Right = null;
}
}
// Inorder Iterator class
class InorderIterator
{
private Stack<Node> traversal;
public InorderIterator(Node root)
{
traversal = new Stack<Node>();
MoveLeft(root);
}
private void MoveLeft(Node current)
{
while (current != null)
{
traversal.Push(current);
current = current.Left;
}
}
public bool HasNext()
{
return traversal.Count > 0;
}
public Node Next()
{
if (!HasNext())
throw new InvalidOperationException("No more elements in the inorder traversal.");
Node current = traversal.Pop();
if (current.Right != null)
MoveLeft(current.Right);
return current;
}
}
// Class to Test given set of inputs
class GFG
{
// Driver Code
static void Main(string[] args)
{
Node root = new Node(8);
root.Right = new Node(9);
root.Left = new Node(3);
root.Left.Left = new Node(2);
root.Left.Right = new Node(4);
root.Left.Right.Right = new Node(5);
InorderIterator itr = new InorderIterator(root);
try
{
Console.Write(itr.Next().Data + " ");
Console.Write(itr.HasNext() + " ");
Console.Write(itr.Next().Data + " ");
Console.Write(itr.Next().Data + " ");
Console.Write(itr.Next().Data + " ");
Console.Write(itr.HasNext() + " ");
Console.Write(itr.Next().Data + " ");
Console.Write(itr.Next().Data + " ");
Console.Write(itr.HasNext() + " ");
}
catch (InvalidOperationException e)
{
Console.Write("No such element Exists");
}
}
}
// This code is contributed by Potta Lokesh
JavaScript
// Javascript Program for above approach
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
// Inorder Iterator class
class InorderIterator {
constructor(root) {
this.traversal = [];
this.moveLeft(root);
}
moveLeft(current) {
while (current != null) {
this.traversal.push(current);
current = current.left;
}
}
hasNext() {
return this.traversal.length > 0;
}
next() {
if (!this.hasNext())
throw new Error('No such element Exists');
let current = this.traversal.pop();
if (current.right != null)
this.moveLeft(current.right);
return current;
}
}
// Class to Test given set of inputs
// Driver Code
let root = new Node(8);
root.right = new Node(9);
root.left = new Node(3);
root.left.left = new Node(2);
root.left.right = new Node(4);
root.left.right.right = new Node(5);
let itr = new InorderIterator(root);
try {
console.log(itr.next().data + " ");
console.log(itr.hasNext() + " ");
console.log(itr.next().data + " ");
console.log(itr.next().data + " ");
console.log(itr.next().data + " ");
console.log(itr.hasNext() + " ");
console.log(itr.next().data + " ");
console.log(itr.next().data + " ");
console.log(itr.hasNext() + " ");
}
catch (e) {
console.log("No such element Exists");
}
// This code is contributed by adityamaharshi21
Output2 true 3 4 5 true 8 9 false
Time Complexity: O(N), Where N is the number of nodes in the binary tree.
Auxiliary Space: O(N), The Stack will hold all N elements in the worst case.
Efficient Approach: Morris Traversal can be used to solve this question using constant space. The idea behind morris traversal is to create a temporary link between a node and the right-most node in its left sub-tree so that the ancestor node can be backtracked. A reference of the ancestor node is set to the right child of the right-most node in its left sub-tree.
Algorithm:
Class is Instantiated
Initialize current = root and rightMost = NULL
hasNext() function
IF current != NULL
return true
ELSE
return false
next() function
- IF current = NULL ( or hasNext() returns false)
- ELSE
- IF current.left = NULL
- Initialize temp = current
- current = current.right
- return temp
- ELSE
- Initialize rightMost = current->left
- while rightMost.right != NULL && rightMost.right != current
- rightMost = rightMost.right
- IF rightMost.right == NULL
- rightMost.right = current
- current = current.left
- ELSE
- temp = current
- rightMost.right = null
- current = current.right
- return current
- Call the function again
Below is the implementation of above approach.
C++
#include <iostream>
#include <stack>
struct Node {
int data;
Node *left, *right;
Node(int data) {
this->data = data;
this->left = this->right = NULL;
}
};
class InorderIterator {
private:
Node *current, *rightMost;
public:
InorderIterator(Node *root) {
current = root;
rightMost = NULL;
}
bool hasNext() { return current != NULL; }
Node* next() {
if (!hasNext()) {
std::cout << "No such element exists" << std::endl;
return NULL;
}
if (current->left == NULL) {
Node *temp = current;
current = current->right;
return temp;
}
rightMost = current->left;
while (rightMost->right != NULL
&& rightMost->right != current) {
rightMost = rightMost->right;
}
if (rightMost->right == NULL) {
rightMost->right = current;
current = current->left;
} else {
rightMost->right = NULL;
Node *temp = current;
current = current->right;
return temp;
}
return next();
}
};
int main() {
Node *root = new Node(8);
root->right = new Node(9);
root->left = new Node(3);
root->left->left = new Node(2);
root->left->right = new Node(4);
root->left->right->right = new Node(5);
InorderIterator itr(root);
std::cout << itr.next()->data << " ";
std::cout << itr.hasNext() << " ";
std::cout << itr.next()->data << " ";
std::cout << itr.next()->data << " ";
std::cout << itr.next()->data << " ";
std::cout << itr.hasNext() << " ";
std::cout << itr.next()->data << " ";
std::cout << itr.next()->data << " ";
std::cout << itr.hasNext() << " ";
return 0;
}
// This code is contributed by anskalyan3.
Java
// Java Program for above approach
import java.util.*;
// Structure of a Node
class Node {
int data;
Node left;
Node right;
Node(int data)
{
this.data = data;
left = right = null;
}
}
// Inorder Iterator class
class InorderIterator {
private Node current, rightMost;
InorderIterator(Node root)
{
current = root;
rightMost = null;
}
public boolean hasNext() { return current != null; }
public Node next()
{
if (!hasNext())
throw new NoSuchElementException();
if (current.left == null) {
Node temp = current;
current = current.right;
return temp;
}
rightMost = current.left;
while (rightMost.right != null
&& rightMost.right != current)
rightMost = rightMost.right;
if (rightMost.right == null) {
rightMost.right = current;
current = current.left;
}
else {
rightMost.right = null;
Node temp = current;
current = current.right;
return temp;
}
return next();
}
}
class Test {
// Driver Code
public static void main(String args[])
{
Node root = new Node(8);
root.right = new Node(9);
root.left = new Node(3);
root.left.left = new Node(2);
root.left.right = new Node(4);
root.left.right.right = new Node(5);
InorderIterator itr = new InorderIterator(root);
try {
System.out.print(itr.next().data + " ");
System.out.print(itr.hasNext() + " ");
System.out.print(itr.next().data + " ");
System.out.print(itr.next().data + " ");
System.out.print(itr.next().data + " ");
System.out.print(itr.hasNext() + " ");
System.out.print(itr.next().data + " ");
System.out.print(itr.next().data + " ");
System.out.print(itr.hasNext() + " ");
}
catch (NoSuchElementException e) {
System.out.print("No such element Exists");
}
}
}
Python
class Node:
def __init__(self, data):
self.data = data
self.left = self.right = None
class InorderIterator:
def __init__(self, root):
self.current = root
self.right_most = None
def has_next(self):
return self.current is not None
def next(self):
if not self.has_next():
print("No such element exists")
return None
if self.current.left is None:
temp = self.current
self.current = self.current.right
return temp
self.right_most = self.current.left
while self.right_most.right is not None and self.right_most.right != self.current:
self.right_most = self.right_most.right
if self.right_most.right is None:
self.right_most.right = self.current
self.current = self.current.left
else:
self.right_most.right = None
temp = self.current
self.current = self.current.right
return temp
return self.next()
root = Node(8)
root.right = Node(9)
root.left = Node(3)
root.left.left = Node(2)
root.left.right = Node(4)
root.left.right.right = Node(5)
itr = InorderIterator(root)
print(itr.next().data)
print(itr.has_next())
print(itr.next().data)
print(itr.next().data)
print(itr.next().data)
print(itr.has_next())
print(itr.next().data)
print(itr.next().data)
print(itr.has_next())
JavaScript
// JavaScript program for above approach
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
class InorderIterator {
constructor(root) {
this.current = root;
this.rightMost = null;
}
hasNext() { return this.current != null; }
next() {
if (!this.hasNext())
throw new Error("No such element Exists");
if (this.current.left == null) {
let temp = this.current;
this.current = this.current.right;
return temp;
}
this.rightMost = this.current.left;
while (this.rightMost.right != null
&& this.rightMost.right != this.current)
this.rightMost = this.rightMost.right;
if (this.rightMost.right == null) {
this.rightMost.right = this.current;
this.current = this.current.left;
}
else {
this.rightMost.right = null;
let temp = this.current;
this.current = this.current.right;
return temp;
}
return this.next();
}
}
let root = new Node(8);
root.right = new Node(9);
root.left = new Node(3);
root.left.left = new Node(2);
root.left.right = new Node(4);
root.left.right.right = new Node(5);
let itr = new InorderIterator(root);
try {
console.log(itr.next().data + " ");
console.log(itr.hasNext() + " ");
console.log(itr.next().data + " ");
console.log(itr.next().data + " ");
console.log(itr.next().data + " ");
console.log(itr.hasNext() + " ");
console.log(itr.next().data + " ");
console.log(itr.next().data + " ");
console.log(itr.hasNext() + " ");
}
catch (e) {
console.log("No such element Exists");
}
// This code is contributed by adityamaharshi21
C#
// C# Program for above approach
using System;
using System.Collections.Generic;
// Structure of a Node
public class Node {
public int data;
public Node left, right;
public Node(int data)
{
this.data = data;
left = right = null;
}
}
// Inorder Iterator class
public class InorderIterator {
private Node current, rightMost;
public InorderIterator(Node root)
{
current = root;
rightMost = null;
}
public bool HasNext() { return current != null; }
public Node Next()
{
if (!HasNext())
throw new Exception("No such element exists");
if (current.left == null) {
Node temp = current;
current = current.right;
return temp;
}
rightMost = current.left;
while (rightMost.right != null
&& rightMost.right != current)
rightMost = rightMost.right;
if (rightMost.right == null) {
rightMost.right = current;
current = current.left;
}
else {
rightMost.right = null;
Node temp = current;
current = current.right;
return temp;
}
return Next();
}
}
public class GFG {
static public void Main()
{
// Code
Node root = new Node(8);
root.right = new Node(9);
root.left = new Node(3);
root.left.left = new Node(2);
root.left.right = new Node(4);
root.left.right.right = new Node(5);
InorderIterator itr = new InorderIterator(root);
try {
Console.Write(itr.Next().data + " ");
Console.Write(itr.HasNext() + " ");
Console.Write(itr.Next().data + " ");
Console.Write(itr.Next().data + " ");
Console.Write(itr.Next().data + " ");
Console.Write(itr.HasNext() + " ");
Console.Write(itr.Next().data + " ");
Console.Write(itr.Next().data + " ");
Console.Write(itr.HasNext() + " ");
}
catch (Exception e) {
Console.Write("No such element Exists");
}
}
}
// This code is contributed by karthik
Output2 true 3 4 5 true 8 9 false
Time Complexity: O(N), where N is the number of nodes in the binary tree. Although we are creating temporary links are created and nodes are traversed multiple times (at most 3 times), the time complexity is still linear.
Auxiliary Space: O(1)
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem