> For the complete documentation index, see [llms.txt](https://mayanktyagi3111.gitbook.io/interview-prep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mayanktyagi3111.gitbook.io/interview-prep/trees/maximum-width-of-a-binary-tree.md).

# Maximum width of a binary tree

Given a binary tree, write a function to get the maximum width of the given tree. Width of a tree is maximum of widths of all levels.

Let us consider the below example tree.

```
         1
        /  \
       2    3
     /  \     \
    4    5     8 
              / \
             6   7
```

For the above tree,\
width of level 1 is 1,\
width of level 2 is 2,\
width of level 3 is 3\
width of level 4 is 2.

So the maximum width of the tree is 3.

```java
class Solution {
    // Using levelOrder traversal
    private static int solveLevelWay(Node root) {
        if (root == null)
            return 0;
        Queue<Node> q = new LinkedList<>();
        q.add(root);
        int maxCount = Integer.MIN_VALUE;
        int count = 1;
        while (q.size() != 0) {
            int newCount = 0;
            int count = q.size();
            while (count-- > 0) {
                Node temp = q.poll();
                if (temp.left != null) {
                    q.add(temp.left);
                    newCount++;
                }
                if (temp.right != null) {
                    q.add(temp.right);
                    newCount++;
                }
            }
            maxCount = Math.max(maxCount, newCount);
        }
        return maxCount;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://mayanktyagi3111.gitbook.io/interview-prep/trees/maximum-width-of-a-binary-tree.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.
