Inorder Traversal in Binary Tree in Java

Inorder Traversal in Binary Tree

Inorder Traversal in Binary Tree in Java is one of the most important tree traversal techniques. It is a Depth First Traversal (DFS) method where nodes are visited in a specific order:

Left → Root → Right

This traversal is especially important because, in a Binary Search Tree (BST), inorder traversal gives the elements in sorted order.

inorder tree traversal

What is Inorder Traversal?

In inorder traversal, we:

  1. Visit the left subtree
  2. Visit the root node
  3. Visit the right subtree

Steps to Find Inorder Traversal in Binary tree​

Here are some of the steps for tree traversal :-
  • Step 1: Traverse the left most child which is 4 in above example and print it.
  • Step 2: Then print it’s parent which is 2 and look for the right child.
  • Step3: Then move to right child and print it i.e. print 5
  • Step 4: Print 1 which is the root  and move to it’s right subtree.
  • Step 5: At last print 3 and as all the nodes get visited , so stop. Therefore ,  the printing sequence will be 4 2 5 1 3 .

Example of Inorder Traversal:

inorder tree traversal in java

Methods for Inorder Traversal in Binary Tree in Java

Inorder traversal in Binary tree can be performed using 2 methods using Java:

Learn DSA

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Methods for Inorder Traversal in Binary Tree

Method 1: Recursive Inorder Traversal

Algorithm for Inorder Traversal

inorder(node)

1. If node is null
      return
2. Call inorder(node.left)
3. Print node.data
4. Call inorder(node.right)

Java Code:

Run
class Node {
    int data;
    Node left, right;

    Node(int value) {
        data = value;
        left = right = null;
    }
}

public class InorderTraversal {

    Node root;

    void inorder(Node node) {

        if (node == null)
            return;

        inorder(node.left);
        System.out.print(node.data + " ");
        inorder(node.right);
    }

    public static void main(String[] args) {

        InorderTraversal tree = new InorderTraversal();

        tree.root = new Node(10);
        tree.root.left = new Node(20);
        tree.root.right = new Node(30);
        tree.root.left.left = new Node(40);
        tree.root.left.right = new Node(50);

        System.out.print("Inorder Traversal: ");
        tree.inorder(tree.root);
    }
}

Output:

Inorder Traversal: 40 20 50 10 30

Method for Inorder Traversal in Binary Tree

Method 2: Iterative Inorder Traversal (Using Stack)

Algorithm for Iterative Inorder Traversal

1. Create an empty stack
2. Set current = root
3. While current is not null OR stack not empty:
a. Push all left nodes into stack
b. Pop from stack
c. Print node
d. Move to right subtree

Java Code:

Run
import java.util.Stack;

class Node {
    int data;
    Node left, right;

    Node(int value) {
        data = value;
        left = right = null;
    }
}

public class InorderIterative {

    Node root;

    void inorder() {

        Stack stack = new Stack<>();
        Node current = root;

        while (current != null || !stack.isEmpty()) {

            // Reach the leftmost node
            while (current != null) {
                stack.push(current);
                current = current.left;
            }

            // Current is null here
            current = stack.pop();
            System.out.print(current.data + " ");

            // Visit right subtree
            current = current.right;
        }
    }

    public static void main(String[] args) {

        InorderIterative tree = new InorderIterative();

        tree.root = new Node(10);
        tree.root.left = new Node(20);
        tree.root.right = new Node(30);
        tree.root.left.left = new Node(40);
        tree.root.left.right = new Node(50);

        System.out.print("Inorder Traversal: ");
        tree.inorder();
    }
}

Output:

Inorder Traversal: 40 20 50 10 30

To wrapping up with

Inorder Traversal in Binary Tree in Java:

  • If root is null → traversal returns nothing
  • Must check null before recursion or stack push
  • Stack should not be empty before pop

Common Problem with Inorder traversal:

  • If root is null → traversal returns nothing
  • Must check null before recursion or stack push
  • Stack should not be empty before pop

Frequently Asked Questions

Answer:

It is a traversal method where nodes are visited in Left → Root → Right order.

Answer:

Time complexity is O(n) because all nodes are visited once.

Answer:

It produces sorted output in Binary Search Trees.

Answer:

Yes, using a stack (iterative method).

Answer:

Space complexity is O(h), where h is the height of the tree.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Stacks

  • Introduction to Stack in Data Structure
    Click Here
  • Operations on a Stack
    Click Here
  • Stack: Infix, Prefix and Postfix conversions
    Click Here
  • Stack Representation in –
    C | C++ | Java
  • Representation of a Stack as an Array. –
    C | C++ | Java
  • Representation of a Stack as a Linked List. –
    C | C++ | Java
  • Infix to Postfix Conversion –
    C | C++ | Java
  • Infix to prefix conversion in –
    C | C++ | Java
  • Postfix to Prefix Conversion in –
    C | C++ | Java

Queues

  • Queues in Data Structures (Introduction)
    Click Here
  • Queues Program in C and implementation
    Click Here
  • Implementation of Queues using Arrays | C Program
    Click Here
  • Types of Queues in Data Structure
    Click Here
  • Application of Queue Data Structure
    Click Here
  • Insertion in Queues Program (Enqueuing) –
    C | C++ | Java
  • Deletion (Removal) in Queues Program(Dequeuing) –
    C | C++ | Java
  • Reverse a Queue –
    C | C++ | Java
  • Queues using Linked Lists –
    C | C++ | Java
  • Implement Queue using Stack –
    C | C++ | Java
  • Implement Queue using two Stacks –
    C | C++ | Java

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction –
    C | C++ | Java
  • Priority Queue Implementation using Array –
    C | C++ | Java
  • Priority Queue using Linked List –
    C | C++ | Java
  • Priority Queue Insertion and Deletion-
    C | C++ | Java

Stacks

Queues

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction – C | C++ | Java
  • Priority Queue Implementation using Array – C | C++ | Java
  • Priority Queue using Linked List – C | C++ | Java
  • Priority Queue Insertion and Deletion- C | C++ | Java