DSA Series Chapter 14 – INORDER TRAVERSAL (BINARY TREE)


DSA Series Chapter 14 – INORDER TRAVERSAL (BINARY TREE)

Objective:

This post explains Inorder Traversal of a Binary Tree in a standalone and detailed manner, suitable for:

  • Engineering examinations

  • Interview preparation

  • Strong recursion and traversal logic building

1. What is Inorder Traversal?

Definition (Exam‑Ready)

Inorder traversal is a depth‑first traversal technique in which the left subtree is visited first, then the root node, and finally the right subtree.

Traversal Order

Left → Root → Right

2. Binary Tree Diagram Used

            1
          /   \
         2     3
        / \
       4   5

The same tree is used for explanation, dry run, and program output.


3. Step‑by‑Step Inorder Traversal on Diagram

Traversal rule: Left → Root → Right

  1. Start at root (1) → move to left subtree

  2. At node 2 → move to its left subtree

  3. At node 4 → left is NULL

    • Visit 4

  4. Right of 4 is NULL → return to node 2

  5. Visit 2

  6. Traverse right subtree of 2

    • Visit 5

  7. Return to root node 1

    • Visit 1

  8. Traverse right subtree of root

    • Visit 3

Final Inorder Output

4 2 5 1 3

4. Algorithm (Recursive – Plain English)

  1. If current node is NULL, return

  2. Traverse left subtree recursively

  3. Visit the current node

  4. Traverse right subtree recursively


5. Recursive Algorithm (Pseudo Code)

INORDER(node)
{
    if(node != NULL)
    {
        INORDER(node->left)
        visit(node)
        INORDER(node->right)
    }
}

6. C Program – Inorder Traversal (Recursive)

#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node *left;
    struct Node *right;
};

struct Node* createNode(int data)
{
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->left = NULL;
    newNode->right = NULL;
    return newNode;
}

void inorder(struct Node* root)
{
    if (root != NULL)
    {
        inorder(root->left);
        printf("%d ", root->data);
        inorder(root->right);
    }
}

int main()
{
    struct Node* root = createNode(1);
    root->left = createNode(2);
    root->right = createNode(3);
    root->left->left = createNode(4);
    root->left->right = createNode(5);

    printf("Inorder Traversal: ");
    inorder(root);

    return 0;
}

7. Dry Run of Recursive Calls

inorder(1)
  inorder(2)
    inorder(4)
      print 4
    print 2
    inorder(5)
      print 5
  print 1
  inorder(3)
    print 3

8. Time and Space Complexity

Time Complexity

  • O(n) — every node is visited once

Space Complexity

  • O(h) — recursion stack, where h = height of tree

Worst case (skewed tree): O(n)


9. Special Property (Very Important Concept)

Inorder Traversal of a BST

Inorder traversal of a Binary Search Tree produces sorted output.

⚠ Note: This property does NOT apply to a normal Binary Tree.


10. Important Applications of Inorder Traversal

  • Retrieving sorted data from BSTs

  • Expression tree evaluation (infix expression)

  • Structural analysis of trees


11. Common Exam & Interview Mistakes

  • Printing root before left subtree (becomes preorder)

  • Assuming inorder output is always sorted

  • Forgetting NULL base condition


12. Practice Questions

  1. Write inorder traversal for a given binary tree diagram.

  2. Predict inorder output for a left‑skewed tree.

  3. Explain why inorder traversal of BST is sorted.

  4. Convert recursive inorder traversal to iterative logic.


13. Learning Outcome (Post 3.2)

After this post, a student can:

  • Explain inorder traversal clearly

  • Dry‑run inorder traversal on any tree

  • Write inorder traversal program in C

  • Distinguish Binary Tree vs BST inorder behavior


ALSO CHECK 

DS(Data Structure) Python&C Edition

DSA Series Chapter 1 Time & Space Complexity ( C Edition)

DSA Series Chapter 2 Arrays  (C Version)


DSA Series Chapter 2 Arrays  (Python Version)

DSA Series Chapter 3 Strings (C Version)


DSA Series Chapter 3 Strings (Python Version)


DSA Series Chapter 4 Linked Lists (Python Version)


DSA Series Chapter 4 Linked Lists (C Version)


DSA Series Chapter 5 Stacks (Python Version)


DSA Series Chapter 5  Stacks  (C Version)




















DSA Series Chapter 13 - Binary Tree traversal, BFS, DFT/DFS, Preorder Traversal In Binary Tree



 







Tail Recursion Optimization: How Compilers Convert Recursion Into Iteration


Advanced Recursion: Memoization vs. Tabulation — The Deep Optimization Blueprint for Professionals


Advanced Sorting Algorithms: Merge Sort Internals — Merge Tree, Cache Behavior & Real Cost Analysis


Enjoyed this post? [Follow here] for daily coding insights.

Comments