C Inorder Traversal

Intermediate
6 min read
Updated: Nov 2026
1 example

Left, Root, Right

Inorder traversal visits the left subtree, the root, then the right subtree. On a binary search tree this produces the keys in ascending order, which makes it the standard way to print or validate a BST.

Remember
void inorder(const struct T *t) {
    if (!t) return;
    inorder(t->left);
    visit(t);
    inorder(t->right);
}

Cheat Sheet

Keep these facts handy while you work with Inorder Traversal in C.

TopicDetail
OrderLeft, root, right
BST resultAscending sorted keys
TimeO(n)
UseSorted output, validation

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Postorder Traversal.

Example 1 — Inorder on a general tree and a BST

Only the BST gives sorted output.

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

struct T {
    int key;
    struct T *left, *right;
};

static struct T *mk(int k, struct T *l, struct T *r) {
    struct T *t = malloc(sizeof *t);
    if (!t) exit(1);
    t->key = k;
    t->left = l;
    t->right = r;
    return t;
}

static void free_tree(struct T *t) {
    if (!t) return;
    free_tree(t->left);
    free_tree(t->right);
    free(t);
}

static void inorder(const struct T *t) {
    if (!t) return;
    inorder(t->left);
    printf(" %d", t->key);
    inorder(t->right);
}

int main(void) {
    struct T *tree = mk(1, mk(2, mk(4, NULL, NULL), mk(5, NULL, NULL)), mk(3, NULL, NULL));
    struct T *bst = mk(4,
        mk(2, mk(1, NULL, NULL), mk(3, NULL, NULL)),
        mk(6, mk(5, NULL, NULL), mk(7, NULL, NULL)));

    printf("binary tree:");
    inorder(tree);
    printf("\nBST:");
    inorder(bst);
    printf("\n");

    free_tree(tree);
    free_tree(bst);
    return 0;
}

How It Works

  1. For the first tree, the left subtree (4, 2, 5) is printed, then the root 1, then 3.
  2. The BST keeps smaller keys left, so the walk is sorted.
  3. If inorder is not sorted, the tree is not a valid BST.

Edge Cases & Pitfalls

Check these before relying on Inorder Traversal in real programs.

sorted

Assuming sorted output on any tree

Only BSTs give ascending order.

dup

Duplicates

BSTs that allow duplicates need a consistent rule (all left or all right).

Key Takeaways

  • Left, root, right.
  • Sorted on a BST.
  • Use it to validate a BST.

One line: Inorder traversal visits the left subtree, the root, then the right subtree.

Frequently Asked Questions

Because all smaller keys are in the left subtree and larger keys in the right, visiting left, root, right yields ascending order.
A traversal that visits the left subtree, then the node, then the right subtree.
Continue with "C Postorder Traversal" next. Visit children before the parent: ideal for freeing a tree. The C Trees and Binary Search Trees hub lists every topic in order.

Next: Postorder Traversal

Visit children before the parent: ideal for freeing a tree.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful