Inorder Traversal is an important tree traversal technique used in Data Structures and Algorithms (DSA). It follows the order Left, Root, Right, which means the left subtree is visited first, followed by the root node and then the right subtree. Inorder traversal is a type of Depth First Search (DFS) and is especially useful when working with Binary Search Trees (BSTs), because it visits their values in ascending sorted order when the standard BST ordering rules are followed. Understanding inorder traversal helps beginners learn tree operations and prepare for technical interviews.
Table of Contents
What Is Inorder Traversal?
Inorder Traversal is a tree traversal technique in which the left subtree is visited first, followed by the root node, and then the right subtree. It follows a specific order: Left → Root → Right
Inorder traversal belongs to Depth-First Search (DFS). It explores the left side of a tree before processing the current node and its right subtree.
In this technique:
- First, traverse the left subtree.
- Next, visit the root node.
- Finally, traverse the right subtree.
The same process is applied recursively to every subtree until all nodes have been visited.
Inorder traversal is mainly used with binary trees because each node can have a left child and a right child.
How Does Inorder Traversal Work?
Inorder traversal starts from the root node but does not visit it immediately. Instead, it first explores the left subtree. After completing the left subtree, it visits the current node and then explores the right subtree.
For every node, the algorithm performs three actions:
- Traverse the left subtree.
- Visit the current node.
- Traverse the right subtree.
If a node does not have a left or right child, the algorithm skips that missing subtree and continues processing.
The process stops when all nodes have been visited.
Steps of Inorder Traversal
The following steps explain how to perform inorder traversal:
- Start from the root node.
- Check whether the current node exists.
- Recursively traverse the left subtree.
- Visit the current node and process its value.
- Recursively traverse the right subtree.
- Continue until all nodes have been visited.
Important Rule: Always visit the left subtree before the root node, and visit the root before the right subtree.
Inorder Traversal Example
Consider a binary tree containing the following values:
- Root node: 10
- Left child: 20
- Right child: 30
- Children of node 20: 40 and 50
Applying in-order traversal follows the Left → Root → Right order.
The visiting sequence is: 40 → 20 → 50 → 10 → 30
Explanation:
- First, visit node 40 because it is the leftmost node.
- Next, visit node 20, which is the parent of nodes 40 and 50.
- Then, visit node 50, the right child of node 20.
- After completing the left subtree, visit the root node 10.
- Finally, visit node 30, the right child of node 10.
The final inorder traversal sequence is:
40, 20, 50, 10, 30
Inorder Traversal Using Recursion
Recursion is one of the simplest ways to implement inorder traversal. A recursive function calls itself to process the left and right subtrees.
JavaScript Example:
// This program performs inorder traversal using recursion.
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
function inorder(root) {
if (root === null) {
return;
}
inorder(root.left);
console.log(root.value);
inorder(root.right);
}
const root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.left.left = new Node(40);
root.left.right = new Node(50);
inorder(root);
Output:
40
20
50
10
30
Explanation:
- The Node class creates individual tree nodes.
- Each node stores a value and references to its left and right children.
- The inorder() function first checks whether the current node is null.
- It then calls itself for the left subtree.
- After completing the left subtree, it prints the current node's value.
- Finally, it calls itself for the right subtree.
The recursive function continues until every node has been visited.
Why Is Recursion Used?
A tree contains smaller subtrees within itself. Recursion makes it easier to apply the same traversal process to each subtree without writing separate code for every level.
Inorder Traversal Using Iteration
Inorder traversal can also be implemented without recursion by using a stack data structure. A stack follows the Last In, First Out (LIFO) principle. The iterative approach uses the stack to remember nodes while exploring their left subtrees.
Steps:
- Create an empty stack.
- Start from the root node.
- Push the current node onto the stack and move to its left child.
- Continue until the current node becomes null.
- Remove the top node from the stack and visit it.
- Move to its right child.
- Repeat until both the current node is null and the stack is empty.
JavaScript Example:
// This program performs inorder traversal using an iterative stack.
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
function inorderIterative(root) {
const stack = [];
let current = root;
while (current !== null || stack.length > 0) {
while (current !== null) {
stack.push(current);
current = current.left;
}
current = stack.pop();
console.log(current.value);
current = current.right;
}
}
const root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.left.left = new Node(40);
root.left.right = new Node(50);
inorderIterative(root);
Output:
40
20
50
10
30
Explanation:
The program first moves as far left as possible while storing nodes in the stack.
- It pushes nodes 10, 20, and 40 into the stack.
- It removes and visits node 40.
- It then visits node 20.
- Next, it visits node 50 from the right subtree of node 20.
- After completing the left subtree, it visits node 10.
- Finally, it visits node 30.
The stack helps maintain the correct inorder sequence without using recursive function calls.
Inorder Traversal in a Binary Search Tree
Inorder traversal is especially important when working with a Binary Search Tree (BST).
A Binary Search Tree is a special binary tree in which smaller values are stored in the left subtree and larger values are stored in the right subtree, following its ordering rules.
When an in-order traversal is applied to a standard BST, the elements are visited in ascending sorted order.
JavaScript Example:
// This program performs inorder traversal of a Binary Search Tree.
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
function insert(root, value) {
if (root === null) {
return new Node(value);
}
if (value < root.value) {
root.left = insert(root.left, value);
} else if (value > root.value) {
root.right = insert(root.right, value);
}
return root;
}
function inorder(root) {
if (root === null) {
return;
}
inorder(root.left);
console.log(root.value);
inorder(root.right);
}
let root = null;
root = insert(root, 50);
root = insert(root, 30);
root = insert(root, 70);
root = insert(root, 20);
root = insert(root, 40);
root = insert(root, 60);
root = insert(root, 80);
inorder(root);
Output:
20
30
40
50
60
70
80
Explanation:
The traversal starts by visiting the leftmost value, 20. It then visits 30 and 40, followed by the root node 50. Finally, it visits the right subtree values 60, 70, and 80.
The result is an ascending sorted sequence.
Important Point:
Inorder traversal produces sorted output for a standard Binary Search Tree. However, it does not necessarily produce sorted output for every binary tree.
Applications of Inorder Traversal
Inorder traversal is useful in several computer science applications.
- Sorting Elements: It retrieves values in ascending order from a standard Binary Search Tree.
- BST Validation: It can help check whether a binary tree follows BST ordering rules.
- Expression Trees: It can generate infix expressions from expression trees.
- Finding Minimum Values: In a standard BST, the leftmost node contains the minimum value.
- Finding Maximum Values: In a standard BST, the rightmost node contains the maximum value.
- Ordered Data Processing: It helps process tree elements in sorted order.
- Tree Analysis: It is useful for examining and processing nodes in binary trees.
Time and Space Complexity
Time and space complexity describe how efficiently an algorithm uses processing time and memory.
Let N represent the total number of nodes in the tree, and H represent its height.
| Complexity | Recursive Method | Iterative Method |
|---|---|---|
| Time Complexity | O(N) | O(N) |
| Auxiliary Space | O(H) | O(H) |
Time Complexity: O(N)
Inorder traversal visits every node exactly once. Therefore, its time complexity is O(N).
Space Complexity:
- Recursive traversal uses O(H) space for the function call stack.
- Iterative traversal uses O(H) space for the explicit stack.
- In a balanced binary tree, the height is approximately O(log N).
- In a highly unbalanced tree, the height can become O(N).
These complexities describe auxiliary space and exclude the memory already occupied by the input tree.
Advantages of Inorder Traversal
Some important advantages of inorder traversal are:
- It is simple to understand and implement.
- It retrieves sorted elements from a standard Binary Search Tree.
- It can be implemented using recursion or iteration.
- It visits every node exactly once.
- It is useful for validating BST ordering.
- It supports expression tree processing.
Common Mistakes to Avoid
- Incorrect Traversal Order: Always follow Left → Root → Right.
- Missing Base Condition: Check whether the current node is null before accessing its value.
- Visiting the Root Too Early: Visit the root only after completing its left subtree.
- Forgetting Empty Trees: Handle cases where the root node is null.
- Assuming Every Binary Tree Produces Sorted Output: Inorder traversal produces sorted output only when the tree follows BST ordering rules.
Frequently Asked Questions
1. What is inorder traversal?
Inorder traversal is a tree traversal technique that visits the left subtree first, followed by the root node and then the right subtree.
2. What is the order of inorder traversal?
The order is Left → Root → Right.
3. Which data structure is used for iterative inorder traversal?
A stack is used to perform iterative inorder traversal.
4. Does inorder traversal produce sorted output?
Yes, inorder traversal produces ascending sorted output when applied to a standard Binary Search Tree.
5. What is the time complexity of inorder traversal?
The time complexity is O(N), where N is the total number of nodes in the tree.
0 Comments