Inorder Traversal of Binary Tree
Last Updated :
28 Mar, 2025
Inorder traversal is a depth-first traversal method that follows this sequence:
- Left subtree is visited first.
- Root node is processed next.
- Right subtree is visited last.
How does Inorder Traversal work?
Key Properties:
- If applied to a Binary Search Tree (BST), it returns elements in sorted order.
- Ensures that nodes are processed in a hierarchical sequence, making it useful for expression trees and BSTs.
Examples
Input:
Output: 2 1 3
Explanation: The Inorder Traversal visits the nodes in the following order: Left, Root, Right. Therefore, we visit the left node 2, then the root node 1 and lastly the right node 3.
Input :
Output: 4 2 5 1 3 6
Explanation: Inorder Traversal (Left → Root → Right). Visit 4 → 2 → 5 → 1 → 3 → 6 , resulting in 4 2 5 1 3 6.
Algorithm :
- If the root is NULL, return.
- Recursively traverse the left subtree.
- Process the root node (e.g., print its value).
- Recursively traverse the right subtree.
C++
#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);
}
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);
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;
}
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);
printInorder(root);
return 0;
}
Java
import java.util.*;
// Structure of a Binary Tree Node
class Node {
int data;
Node left, right;
Node(int v)
{
data = v;
left = right = null;
}
}
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);
}
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);
printInorder(root);
}
}
Python
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)
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)
printInorder(root)
C#
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;
}
}
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);
}
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);
printInorder(root);
}
}
JavaScript
// 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);
}
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);
printInorder(root);
Time Complexity: O(n), n is the total number of nodes
Auxiliary Space: O(h), h is the height of the tree.
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)
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
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
Preorder Traversal of Binary Tree Preorder traversal is a tree traversal method that follows the Root-Left-Right order:The root node of the subtree is visited first.Next, the left subtree is recursively traversed.Finally, the right subtree is recursively traversed.How does Preorder Traversal work?Key Properties: Used in expression t
5 min read
Postorder Traversal of Binary Tree Postorder traversal is a tree traversal method that follows the Left-Right-Root order:The left subtree is visited first.The right subtree is visited next.The root node is processed last.How does Postorder Traversal work?Key Properties:It is used for tree deletion because subtrees are deleted before
5 min read
Mix Order Traversal of a Binary Tree Given a Binary Tree consisting of N nodes, the task is to print its Mix Order Traversal. Mix Order Traversal is a tree traversal technique, which involves any two of the existing traversal techniques like Inorder, Preorder and Postorder Traversal. Any two of them can be performed or alternate levels
13 min read