# Level Order Traversal

**The below function requires a queue data structure of type pointer to Node to be created and initialised.**&#x20;

```c
void levelOrder(struct Node *ptr) {

    // Queue structure to store pointer
    struct Queue q;
    
    // Print the root node as it is on the topmost level
    printf("%d\t", ptr -> data);
    // Enqueue root node to the queue
    enqueue(&q, ptr);
    
    while (!isEmpty(q)) {
        // Obtain root of subtree
        ptr = dequeue(&q);
        // Print & enqueue the left child of next level if present
        if (ptr -> left) {
            printf("%d\t", ptr -> left -> data);
            enqueue(&q, ptr -> left);
        }
        // Print & enqueue the left child of next level if present
        if (ptr -> right) {
            printf("%d\t", ptr -> right -> data);
            enqueue(&q, ptr -> right); 
        }
    }

}
```

Contributed by Nitin Ranganath


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nitinranganath.gitbook.io/data-structures/binary-tree/level-order-traversal.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
