# Creating a Binary Tree using Queue

**Note : The below code assumes that a queue of type pointer to Node is already created and initialised.**&#x20;

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

struct Node *root = NULL;

void createTree(int noOfNodes) {

    // Pointer to keep track of current node and for new nodes
    struct Node *ptr, *newNode;
    int data;
    
    // A queue to store addresses of each node
    struct Queue q;
    create(&q, noOfNodes);
    
    // Create the root node and make its child NULL by default
    printf("Enter the value of root\n");
    scanf("%d", &data);
    root = (struct Node *)malloc(sizeof(struct Node));
    root -> data = data;
    root -> left = root -> right = NULL;
    // Enqueue the root to queue
    
    // Repeat until no node is to be inserted further
    while (isEmpty(q)) {
        
        // Get address of current node
        ptr = dequque(&q);
        
        // Inserting left child node
        printf("Enter the left child of %d:\n", ptr -> data);
        scanf("%d", &data);
        if (data != -1) {
            newNode = (struct Node *)malloc(sizeof(struct Node));
            newNode -> data = data;
            newNode -> left = newNode -> right = NULL;
            ptr -> left = newNode;
        }
        
        // Inserting right child node
        printf("Enter the right child of %d:\n", ptr -> data);
        scanf("%d", &data);
        if (data != -1) {
            newNode = (struct Node *)malloc(sizeof(struct Node));
            newNode -> data = data;
            newNode -> left = newNode -> right = NULL;
            ptr -> right = newNode;
        }
    }

}
```

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/creating-a-binary-tree-using-queue.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.
