Inorder Traversal of Binary Tree
Last Updated :
21 Oct, 2024
Inorder traversal is defined as a type of tree traversal technique which follows the Left-Root-Right pattern, such that:
- The left subtree is traversed first
- Then the root node for that subtree is traversed
- Finally, the right subtree is traversed

Examples of Inorder Traversal
Input:

Output: BAC
Explanation: The Inorder Traversal visits the nodes in the following order: Left, Root, Right. Therefore, we visit the left node B, then the root node A and lastly the right node C.
Input :

Output: DBEAC
Input: NULL
Output:
Output is empty in this case.
Algorithm for Inorder Traversal
Inorder(root):
- If root is NULL, then return
- Inorder (root -> left)
- Process root (For example, print rootâs data)
- Inorder (root -> right)
Program to implement Inorder Traversal of Binary Tree:
Below is the code implementation of the inorder traversal:
C++
// C++ program for inorder traversals
#include <bits/stdc++.h>
using namespace std;
// Structure of a Binary Tree Node
struct Node {
int data;
struct Node *left, *right;
Node(int v)
{
data = v;
left = right = nullptr;
}
};
// Function to print inorder traversal
void printInorder(struct Node* node)
{
if (node == nullptr)
return;
// First recur on left subtree
printInorder(node->left);
// Now deal with the node
cout << node->data << " ";
// Then recur on right subtree
printInorder(node->right);
}
// Driver code
int main()
{
struct Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
root->right->right = new Node(6);
// Function call
cout << "Inorder traversal of binary tree is: \n";
printInorder(root);
return 0;
}
C
#include <stdio.h>
#include <stdlib.h>
// Structure of a Binary Tree Node
struct Node {
int data;
struct Node *left, *right;
};
// Function to print inorder traversal
void printInorder(struct Node* node) {
if (node == NULL)
return;
// First recur on left subtree
printInorder(node->left);
// Now deal with the node
printf("%d ", node->data);
// Then recur on right subtree
printInorder(node->right);
}
// Function to create a new node
struct Node* newNode(int v) {
struct Node* node =
(struct Node*)malloc(sizeof(struct Node));
node->data = v;
node->left = node->right = NULL;
return node;
}
// Driver code
int main() {
struct Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->right = newNode(6);
// Function call
printf("Inorder traversal of binary tree is: \n");
printInorder(root);
return 0;
}
Java
// Java program for inorder traversals
import java.util.*;
// Structure of a Binary Tree Node
class Node {
int data;
Node left, right;
Node(int v)
{
data = v;
left = right = null;
}
}
// Main class
class GFG {
// Function to print inorder traversal
public static void printInorder(Node node)
{
if (node == null)
return;
// First recur on left subtree
printInorder(node.left);
// Now deal with the node
System.out.print(node.data + " ");
// Then recur on right subtree
printInorder(node.right);
}
// Driver code
public static void main(String[] args)
{
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.right = new Node(6);
// Function call
System.out.println(
"Inorder traversal of binary tree is: ");
printInorder(root);
}
}
// This code is contributed by prasad264
Python
# Structure of a Binary Tree Node
class Node:
def __init__(self, v):
self.data = v
self.left = None
self.right = None
# Function to print inorder traversal
def printInorder(node):
if node is None:
return
# First recur on left subtree
printInorder(node.left)
# Now deal with the node
print(node.data, end=' ')
# Then recur on right subtree
printInorder(node.right)
# Driver code
if __name__ == '__main__':
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.right.right = Node(6)
# Function call
print("Inorder traversal of binary tree is:")
printInorder(root)
C#
// C# program for inorder traversals
using System;
// Structure of a Binary Tree Node
public class Node {
public int data;
public Node left, right;
public Node(int v)
{
data = v;
left = right = null;
}
}
// Class to store and print inorder traversal
public class BinaryTree {
// Function to print inorder traversal
public static void printInorder(Node node)
{
if (node == null)
return;
// First recur on left subtree
printInorder(node.left);
// Now deal with the node
Console.Write(node.data + " ");
// Then recur on right subtree
printInorder(node.right);
}
// Driver code
public static void Main()
{
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.right = new Node(6);
// Function call
Console.WriteLine(
"Inorder traversal of binary tree is: ");
printInorder(root);
}
}
JavaScript
// JavaScript program for inorder traversals
// Structure of a Binary Tree Node
class Node {
constructor(v) {
this.data = v;
this.left = null;
this.right = null;
}
}
// Function to print inorder traversal
function printInorder(node) {
if (node === null) {
return;
}
// First recur on left subtree
printInorder(node.left);
// Now deal with the node
console.log(node.data);
// Then recur on right subtree
printInorder(node.right);
}
// Driver code
const root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.right = new Node(6);
// Function call
console.log("Inorder traversal of binary tree is: ");
printInorder(root);
OutputInorder traversal of binary tree is:
4 2 5 1 3 6
Time Complexity: O(N) where N is the total number of nodes. Because it traverses all the nodes at least once.
Auxiliary Space: O(h) where h is the height of the tree. This space is required for recursion calls.
- In the worst case, h can be the same as N (when the tree is a skewed tree)
- In the best case, h can be the same as log N (when the tree is a complete tree
How does Inorder Traversal of Binary Tree work?
Let us understand the algorithm with the below example tree

Example of Binary Tree
If we perform an inorder traversal in this binary tree, then the traversal will be as follows:
Step 1: The traversal will go from 1 to its left subtree i.e., 2, then from 2 to its left subtree root, i.e., 4. Now 4 has no left subtree, so it will be visited. It also does not have any right subtree. So no more traversal from 4

Node 4 is visited
Step 2: As the left subtree of 2 is visited completely, now it read data of node 2 before moving to its right subtree.

Node 2 is visited
Step 3: Now the right subtree of 2 will be traversed i.e., move to node 5. For node 5 there is no left subtree, so it gets visited and after that, the traversal comes back because there is no right subtree of node 5.

Node 5 is visited
Step 4: As the left subtree of node 1 is, the root itself, i.e., node 1 will be visited.

Node 1 is visited
Step 5: Left subtree of node 1 and the node itself is visited. So now the right subtree of 1 will be traversed i.e., move to node 3. As node 3 has no left subtree so it gets visited.

Node 3 is visited
Step 6: The left subtree of node 3 and the node itself is visited. So traverse to the right subtree and visit node 6. Now the traversal ends as all the nodes are traversed.

The complete tree is traversed
So the order of traversal of nodes is 4 -> 2 -> 5 -> 1 -> 3 -> 6.
Use cases of Inorder Traversal:
In the case of BST (Binary Search Tree), if any time there is a need to get the nodes in increasing order
Related Articles:
Similar Reads
Inorder traversal of an N-ary Tree
Given an N-ary tree containing, the task is to print the inorder traversal of the tree. Examples:Â Input: N = 3Â Â Output: 5 6 2 7 3 1 4Input: N = 3Â Â Output: 2 3 5 1 4 6Â Approach: The inorder traversal of an N-ary tree is defined as visiting all the children except the last then the root and finall
6 min read
Boundary Traversal of binary tree
Given a binary tree, the task is to find the boundary nodes of the binary tree Anti-Clockwise starting from the root. The boundary includes: left boundary (nodes on left excluding leaf nodes)leaves (consist of only the leaf nodes)right boundary (nodes on right excluding leaf nodes)The left boundary
15+ min read
Binary Tree Iterator for Inorder Traversal
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 \
13 min read
Diagonal Traversal of Binary Tree
Given a Binary Tree, the task is to print the diagonal traversal of the binary tree.Note: If the diagonal element are present in two different subtrees, then left subtree diagonal element should be taken first and then right subtree. Example: Input: Output: 8 10 14 3 6 7 13 1 4Explanation: The above
7 min read
Binary Tree Traversal
Binary trees are fundamental data structures in computer science and understanding their traversal is crucial for various applications. Traversing a binary tree means visiting all the nodes in a specific order. There are several traversal methods, each with its unique applications and benefits. This
15+ min read
Double Order Traversal of a Binary Tree
Given a Binary Tree, the task is to find its Double Order Traversal. Double Order Traversal is a tree traversal technique in which every node is traversed twice in the following order: Visit the Node.Traverse the Left Subtree.Visit the Node.Traverse the Right Subtree.Examples: Input: Output: 1 7 4 4
6 min read
Triple Order Traversal of a Binary Tree
Given a Binary Tree, the task is to find its Triple Order Traversal. Triple Order Traversal is a tree traversal technique in which every node is traversed thrice in the following order: Visit the root nodeTraverse the left subtreeVisit the root nodeTraverse the right subtreeVisit the root node.Examp
7 min read
DFS traversal of a Tree
Given a Binary tree, Traverse it using DFS using recursion. Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to traverse them, trees can be traversed in different ways. Generally, there are 2 widely used ways for traversing trees: DFS or Depth-F
15+ min read
Density of Binary Tree in One Traversal
Given a Binary Tree, the task is to find the density of it by doing one traversal of it. Density of Binary Tree = (Size / Height). Examples: Input: Output: 1.5Explanation: As the height of given tree is 2 and size of given tree is 3 , the density is (3/2) = 1.5 Input: Output: 1Explanation: As the he
6 min read
Specific Level Order Traversal of Binary Tree
Given a Binary Tree, the task is to perform a Specific Level Order Traversal of the tree such that at each level print 1st element then the last element, then 2nd element and 2nd last element, until all elements of that level are printed and so on. Examples: Input: Output: 5 3 7 2 8 4 6 9 0 5 1 Expl
8 min read