VOOZH about

URL: https://www.geeksforgeeks.org/c/level-order-traversal-in-c/

⇱ Level Order Traversal in C - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Level Order Traversal in C

Last Updated : 23 Jul, 2025

The Level Order Traversal is a traversal technique used to traverse a tree level by level from top to bottom and from left to right at each level of a tree. In this article, we will learn how to implement level order traversal of a binary tree in C using two different methods.

Level order traversal of a Binary Tree

A is a hierarchical data structure where each node can have at most two children: a left child and a right child. Level order traversal allows us to process each node of the binary tree level by level from left to right. Generally, level order traversal is implemented using , but it can also be implemented without queues using . Let's look at an example to understand how level order traversal works.

👁 Producer
Level Order Traversal in Tree

Implementation of Level Order Traversal in C

There are 2 methods through which we can implement the level order traversal of a tree in C. The methods are:

  • Recursive Method
  • Level Order traversal using queue.(Iteraive Method)

Recursive Level Order Traversal

To implement the recursive level traversal we can follow the below approach:

Approach

  • Define a function height to calculate the height of the tree.
  • Define a function printGivenLevel to recursively print nodes at a given level of the tree.
  • Iteratre through each level of the tree starting from 1 to calculated height.
  • Call the printGivenLevel function to print the nodes of that level.

C Program for Recursive Level Order Traversal

The following program illustrates how we can implement the recursive level order traversal for a tree in C:


Output
Level Order Traversal:
1 2 3 4 5 

Time Complexity: O(N), where N denotes the number of nodes in the binary tree.
Auxiliary Space: O(1). The space complexity will be O(N) if the recusive stack space is considered. Because each recursive call adds a new frame to the call stack and if the recursion depth is proportional to the size of the input then the space complexity of the call stack will be O(N).

Level Order Traversal using Queue

We will follow the below approach to implement the level order traversal using queue:

Approach

  • Define the necessary data structure QueNode and Queue for implementing a queue which will be used for the level order traversal of the tree.
  • Create an empty queue.
  • Enqueue the root node of the tree to start the level order traversal process.
  • While the queue is not empty:
    • Dequeue a node from the front of the queue.
    • Print the value of the dequeued node.
    • Enqueue the node's left child into the queue if it is present.
    • Enqueue the node's right child into the queue if it is present.
  • Free the memory allocated for the queue.

C Program for Level Order Traversal using Queue

The following program demonstrates how we can implement the level order traversal of a binary tree using queue in C:


Output
Level Order Traversal:
1 2 3 4 5 

Time Complexity: O(N), where N denotes the number of nodes in the binary tree.
Auxiliary Space: O(N)

Comment