C Level-order Traversal

Intermediate
6 min read
Updated: Nov 2026
1 example

Breadth-First by Level

Level-order traversal (breadth-first) visits all nodes at depth 0, then depth 1, and so on. It uses a queue: dequeue a node, process it, and enqueue its children. Processing the queue in chunks equal to the current size separates the levels.

Remember
enqueue(root);
while (!empty) {
    n = dequeue();
    visit(n);
    if (n->left)  enqueue(n->left);
    if (n->right) enqueue(n->right);
}

Cheat Sheet

Keep these facts handy while you work with Level-order Traversal in C.

TopicDetail
OrderLevel by level
StructureQueue
TimeO(n)
SpaceO(width) of the tree

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Binary Search Trees in C.

Example 1 — Print each level on its own line

Use the queue size at the start of each round.

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 level_order(const struct T *root) {
    const struct T *q[32];
    int head = 0, tail = 0, level = 0;
    if (!root) return;
    q[tail++] = root;

    while (head < tail) {
        int n = tail - head;
        printf("level %d:", level++);
        for (int i = 0; i < n; i++) {
            const struct T *t = q[head++];
            printf(" %d", t->key);
            if (t->left)  q[tail++] = t->left;
            if (t->right) q[tail++] = t->right;
        }
        printf("\n");
    }
}

int main(void) {
    struct T *root = mk(1, mk(2, mk(4, NULL, NULL), mk(5, NULL, NULL)), mk(3, NULL, NULL));
    level_order(root);
    free_tree(root);
    return 0;
}

How It Works

  1. The queue starts with the root.
  2. Each round handles exactly the nodes that were queued for that level.
  3. Children enqueued during a round form the next level.

Edge Cases & Pitfalls

Check these before relying on Level-order Traversal in real programs.

size

Queue too small

A wide tree needs a queue as large as its widest level.

null

Enqueuing NULL

Only enqueue children that exist.

Key Takeaways

  • Queue drives BFS.
  • Chunk by queue size for levels.
  • Space is the tree width.

One line: Level-order traversal (breadth-first) visits all nodes at depth 0, then depth 1, and so on.

Frequently Asked Questions

A breadth-first traversal that visits nodes level by level from the root downward.
A queue.
Continue with "Binary Search Trees in C" next. The BST property, minimum, maximum, and sorted traversal. The C Trees and Binary Search Trees hub lists every topic in order.

Next: Binary Search Trees in C

The BST property, minimum, maximum, and sorted traversal.

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.

9 people found this page helpful