Open In App

Inorder Traversal of Binary Tree

Last Updated : 21 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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
How inorder traversal works

Examples of Inorder Traversal

Input:

postorder_traversal_of_binary_tree

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 :

postorder_traversal_of_binary_tree_1

Output: DBEAC


Input: NULL
Output:
Output is empty in this case.

Algorithm for Inorder Traversal

Inorder(root):

  1. If root is NULL, then return
  2. Inorder (root -> left)
  3. Process root (For example, print root’s data)
  4. 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 Java Python C# JavaScript

Output
Inorder 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

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

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

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

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

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

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

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:



Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg