Maximum width of a Binary Tree

Last Updated : 17 Jul, 2026

Given the root of a binary tree, find its maximum width. The maximum width of a binary tree is defined as the maximum number of nodes present at any level of the tree.

Example: 

Input: root = [1, 2, 3, 4, 5, 6, 7]

blobid0_1754464633

Output: 4
Explanation: On the first level there is only one node [1]. On the second level there are two nodes [2, 3]. On the third level there are 4 nodes [4, 5, 6, 7], clearly it is the maximum number of nodes at any level.

Input: root = [10, 20, 30, 40, 60]

blobid1_1756989561

Output: 2
Explanation: There is one node on level 1(10) There is two node on level 2(20, 30) There is two node on level 3(40, 60) Hence the answer is 2

Try It Yourself
redirect icon

[Naive Approach] By Counting Nodes at Each Level- O(n^2) Time and O(h) Space

The idea is to first find the height of the tree, then for each level, count the number of nodes, and finally take the maximum of these counts as the width.

  • Compute the height of the tree using recursion.
  • For each level from 1 to height:
  • Call a helper function that counts nodes at that level.
  • Update the maximum width after checking each level.
C++
#include <iostream>
#include <algorithm>
using namespace std;

// Structure of a binary tree node
class Node {
public:
    int data;
    Node* left;
    Node* right;

    Node(int x) {
        data = x;
        left = nullptr;
        right = nullptr;
    }
};

// Returns the height of the tree
int height(Node* root) {
    if (root == nullptr)
        return 0;

    return 1 + max(height(root->left), height(root->right));
}

// Counts nodes at the given level
int getWidth(Node* root, int level) {
    if (root == nullptr)
        return 0;

    if (level == 1)
        return 1;

    return getWidth(root->left, level - 1) +
           getWidth(root->right, level - 1);
}

// Returns the maximum width of the binary tree
int maxWidth(Node* root) {
    int h = height(root);
    int ans = 0;

    for (int i = 1; i <= h; i++) {
        ans = max(ans, getWidth(root, i));
    }

    return ans;
}

int main() {

    // root = [1, 2, 3, 4, 5, 6, 7]
    // Construct the following binary tree:
    //         1
    //       /   \
    //      2     3
    //     / \   / \
    //    4   5 6   7

    Node* root = new Node(1);

    root->left = new Node(2);
    root->right = new Node(3);

    root->left->left = new Node(4);
    root->left->right = new Node(5);

    root->right->left = new Node(6);
    root->right->right = new Node(7);

    cout << maxWidth(root) << endl;

    return 0;
}
Java
// Structure of a binary tree node
class Node {
    int data;
    Node left;
    Node right;

    Node(int x) {
        data = x;
        left = null;
        right = null;
    }
}

class GFG {
    
    // Returns the height of the tree
    public static int height(Node root) {
        if (root == null)
            return 0;

        return 1 + Math.max(height(root.left), height(root.right));
    }

    // Counts nodes at the given level
    public static int getWidth(Node root, int level) {
        if (root == null)
            return 0;

        if (level == 1)
            return 1;

        return getWidth(root.left, level - 1) +
               getWidth(root.right, level - 1);
    }

    // Returns the maximum width of the binary tree
   public static int maxWidth(Node root) {
        int h = height(root);
        int ans = 0;

        for (int i = 1; i <= h; i++) {
            ans = Math.max(ans, getWidth(root, i));
        }

        return ans;
    }

    public static void main(String[] args) {

        // root = [1, 2, 3, 4, 5, 6, 7]
        // Construct the following binary tree:
        //         1
        //       /   \
        //      2     3
        //     / \   / \
        //    4   5 6   7

        Node root = new Node(1);

        root.left = new Node(2);
        root.right = new Node(3);

        root.left.left = new Node(4);
        root.left.right = new Node(5);

        root.right.left = new Node(6);
        root.right.right = new Node(7);

        System.out.println(maxWidth(root));
    }
}
Python
# Structure of a binary tree node
class Node:

    def __init__(self, x):
        self.data = x
        self.left = None
        self.right = None


# Returns the height of the tree
def height(root):
    if root == None:
        return 0

    return 1 + max(height(root.left), height(root.right))


# Counts nodes at the given level
def getWidth(root, level):
    if root == None:
        return 0

    if level == 1:
        return 1

    return getWidth(root.left, level - 1) + getWidth(root.right, level - 1)


# Returns the maximum width of the binary tree
def maxWidth(root):
    h = height(root)
    ans = 0

    for i in range(1, h + 1):
        ans = max(ans, getWidth(root, i))

    return ans


if __name__ == "__main__":

    # root = [1, 2, 3, 4, 5, 6, 7]
    # Construct the following binary tree:
    #         1
    #       /   \
    #      2     3
    #     / \   / \
    #    4   5 6   7

    root = Node(1)

    root.left = Node(2)
    root.right = Node(3)

    root.left.left = Node(4)
    root.left.right = Node(5)

    root.right.left = Node(6)
    root.right.right = Node(7)

    print(maxWidth(root))
C#
using System;

// Structure of a binary tree node
class Node {
    public int data;
    public Node left;
    public Node right;

    public Node(int x) {
        data = x;
        left = null;
        right = null;
    }
}

class GFG {

    // Returns the height of the tree
    static int height(Node root) {
        if (root == null)
            return 0;

        return 1 + Math.Max(height(root.left), height(root.right));
    }

    // Counts nodes at the given level
    static int getWidth(Node root, int level) {
        if (root == null)
            return 0;

        if (level == 1)
            return 1;

        return getWidth(root.left, level - 1) +
               getWidth(root.right, level - 1);
    }

    // Returns the maximum width of the binary tree
    static int maxWidth(Node root) {
        int h = height(root);
        int ans = 0;

        for (int i = 1; i <= h; i++) {
            ans = Math.Max(ans, getWidth(root, i));
        }

        return ans;
    }

    public static void Main() {

        // root = [1, 2, 3, 4, 5, 6, 7]
        // Construct the following binary tree:
        //         1
        //       /   \
        //      2     3
        //     / \   / \
        //    4   5 6   7

        Node root = new Node(1);

        root.left = new Node(2);
        root.right = new Node(3);

        root.left.left = new Node(4);
        root.left.right = new Node(5);

        root.right.left = new Node(6);
        root.right.right = new Node(7);

        Console.WriteLine(maxWidth(root));
    }
}
JavaScript
// Structure of a binary tree node
class Node {
    constructor(x) {
        this.data = x;
        this.left = null;
        this.right = null;
    }
}

// Returns the height of the tree
function height(root) {
    if (root == null)
        return 0;

    return 1 + Math.max(height(root.left), height(root.right));
}

// Counts nodes at the given level
function getWidth(root, level) {
    if (root == null)
        return 0;

    if (level == 1)
        return 1;

    return getWidth(root.left, level - 1) +
           getWidth(root.right, level - 1);
}

// Returns the maximum width of the binary tree
function maxWidth(root) {
    let h = height(root);
    let ans = 0;

    for (let i = 1; i <= h; i++) {
        ans = Math.max(ans, getWidth(root, i));
    }

    return ans;
}

// Driver code

// root = [1, 2, 3, 4, 5, 6, 7]
// Construct the following binary tree:
//         1
//       /   \
//      2     3
//     / \   / \
//    4   5 6   7

let root = new Node(1);

root.left = new Node(2);
root.right = new Node(3);

root.left.left = new Node(4);
root.left.right = new Node(5);

root.right.left = new Node(6);
root.right.right = new Node(7);

console.log(maxWidth(root));

Output
4

[Expected Approach 1] Using Level Order Traversal - O(n) Time and O(n) Space

The idea is to use level order traversal (BFS) and count the number of nodes at each level. The maximum count across all levels will be the width of the tree.

  • If root is NULL, return 0.
  • Insert the root node into a queue.
  • Traverse the tree level by level:
  • Get the number of nodes at the current level.
  • Update the maximum width.
  • Add children of current level nodes to the queue.
  • Return the maximum width.
C++
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;

// Structure of a binary tree node
class Node {
public:
    int data;
    Node* left;
    Node* right;

    Node(int x) {
        data = x;
        left = nullptr;
        right = nullptr;
    }
};

// Returns the maximum width of the binary tree
int maxWidth(Node* root) {

    if (root == nullptr)
        return 0;

    queue<Node*> q;
    q.push(root);

    int ans = 0;

    while (!q.empty()) {

        // Number of nodes at current level
        int count = q.size();

        // Update maximum width
        ans = max(ans, count);

        // Process all nodes of current level
        while (count--) {

            Node* curr = q.front();
            q.pop();

            // Add left child
            if (curr->left)
                q.push(curr->left);

            // Add right child
            if (curr->right)
                q.push(curr->right);
        }
    }

    return ans;
}

int main() {

    // root = [1, 2, 3, 4, 5, 6, 7]
    //
    // Construct the following binary tree:
    //         1
    //       /   \
    //      2     3
    //     / \   / \
    //    4   5 6   7

    Node* root = new Node(1);

    root->left = new Node(2);
    root->right = new Node(3);

    root->left->left = new Node(4);
    root->left->right = new Node(5);

    root->right->left = new Node(6);
    root->right->right = new Node(7);

    cout << maxWidth(root);

    return 0;
}
Java
import java.util.Queue;
import java.util.LinkedList;

// Structure of a binary tree node
class Node {
    int data;
    Node left;
    Node right;

    Node(int x) {
        data = x;
        left = null;
        right = null;
    }
}

class GFG {

    // Returns the maximum width of the binary tree
     public static int maxWidth(Node root) {

        if (root == null)
            return 0;

        Queue<Node> q = new LinkedList<>();
        q.add(root);

        int ans = 0;

        while (!q.isEmpty()) {

            // Number of nodes at current level
            int count = q.size();

            // Update maximum width
            ans = Math.max(ans, count);

            // Process all nodes of current level
            while (count-- > 0) {

                Node curr = q.poll();

                // Add left child
                if (curr.left != null)
                    q.add(curr.left);

                // Add right child
                if (curr.right != null)
                    q.add(curr.right);
            }
        }

        return ans;
    }

    public static void main(String[] args) {

        // root = [1, 2, 3, 4, 5, 6, 7]
        //
        // Construct the following binary tree:
        //         1
        //       /   \
        //      2     3
        //     / \   / \
        //    4   5 6   7

        Node root = new Node(1);

        root.left = new Node(2);
        root.right = new Node(3);

        root.left.left = new Node(4);
        root.left.right = new Node(5);

        root.right.left = new Node(6);
        root.right.right = new Node(7);

        System.out.println(maxWidth(root));
    }
}
Python
from collections import deque

# Structure of a binary tree node
class Node:

    def __init__(self, x):
        self.data = x
        self.left = None
        self.right = None


# Returns the maximum width of the binary tree
def maxWidth(root):

    if root == None:
        return 0

    q = deque()
    q.append(root)

    ans = 0

    while len(q) > 0:

        # Number of nodes at current level
        count = len(q)

        # Update maximum width
        ans = max(ans, count)

        # Process all nodes of current level
        while count > 0:

            curr = q.popleft()
            count -= 1

            # Add left child
            if curr.left:
                q.append(curr.left)

            # Add right child
            if curr.right:
                q.append(curr.right)

    return ans


if __name__ == "__main__":

    # root = [1, 2, 3, 4, 5, 6, 7]
    #
    # Construct the following binary tree:
    #         1
    #       /   \
    #      2     3
    #     / \   / \
    #    4   5 6   7

    root = Node(1)

    root.left = Node(2)
    root.right = Node(3)

    root.left.left = Node(4)
    root.left.right = Node(5)

    root.right.left = Node(6)
    root.right.right = Node(7)

    print(maxWidth(root))
C#
using System;
using System.Collections.Generic;

// Structure of a binary tree node
class Node {
    public int data;
    public Node left;
    public Node right;

    public Node(int x) {
        data = x;
        left = null;
        right = null;
    }
}

class GFG {

    // Returns the maximum width of the binary tree
     public static int maxWidth(Node root) {

        if (root == null)
            return 0;

        Queue<Node> q = new Queue<Node>();
        q.Enqueue(root);

        int ans = 0;

        while (q.Count > 0) {

            // Number of nodes at current level
            int count = q.Count;

            // Update maximum width
            ans = Math.Max(ans, count);

            // Process all nodes of current level
            while (count-- > 0) {

                Node curr = q.Dequeue();

                // Add left child
                if (curr.left != null)
                    q.Enqueue(curr.left);

                // Add right child
                if (curr.right != null)
                    q.Enqueue(curr.right);
            }
        }

        return ans;
    }

    public static void Main() {

        // root = [1, 2, 3, 4, 5, 6, 7]
        //
        // Construct the following binary tree:
        //         1
        //       /   \
        //      2     3
        //     / \   / \
        //    4   5 6   7

        Node root = new Node(1);

        root.left = new Node(2);
        root.right = new Node(3);

        root.left.left = new Node(4);
        root.left.right = new Node(5);

        root.right.left = new Node(6);
        root.right.right = new Node(7);

        Console.WriteLine(maxWidth(root));
    }
}
JavaScript
// Structure of a binary tree node
class Node {
    constructor(x) {
        this.data = x;
        this.left = null;
        this.right = null;
    }
}

// Returns the maximum width of the binary tree
function maxWidth(root) {

    if (root == null)
        return 0;

    let q = [];
    q.push(root);

    let ans = 0;

    while (q.length > 0) {

        // Number of nodes at current level
        let count = q.length;

        // Update maximum width
        ans = Math.max(ans, count);

        // Process all nodes of current level
        while (count-- > 0) {

            let curr = q.shift();

            // Add left child
            if (curr.left)
                q.push(curr.left);

            // Add right child
            if (curr.right)
                q.push(curr.right);
        }
    }

    return ans;
}

// Driver code

// root = [1, 2, 3, 4, 5, 6, 7]
//
// Construct the following binary tree:
//         1
//       /   \
//      2     3
//     / \   / \
//    4   5 6   7

let root = new Node(1);

root.left = new Node(2);
root.right = new Node(3);

root.left.left = new Node(4);
root.left.right = new Node(5);

root.right.left = new Node(6);
root.right.right = new Node(7);

console.log(maxWidth(root));

Output
4

[Expected Approach 2] Using Preorder Traversal - O(n) Time and O(n) Space

Instead of traversing the tree level by level, we can count nodes at each level using a preorder traversal. Maintain an array count, where the index represents the level of the tree and the value stores the number of nodes present at that level. During preorder traversal, whenever we visit a node, we increase the count of its corresponding level.

After visiting all nodes, the maximum value in the count array represents the maximum width of the binary tree.

  • Create a temporary array count of size equal to the height of the tree. 
  • Initialize all values in count as 0. 
  • Traverse the tree using preorder traversal and fill the entries in count so that 
  • The count array contains the count of nodes at each level of the Binary Tree.
  • The level with the maximum number of nodes has the maximum width.
  • Return the value of that level. 
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// Structure of a binary tree node
class Node {
public:
    int data;
    Node* left;
    Node* right;

    Node(int x) {
        data = x;
        left = nullptr;
        right = nullptr;
    }
};

// Preorder traversal to count nodes at each level
void preorder(Node* root, int level, vector<int>& count) {

    if (root == nullptr)
        return;

    // Add new level in count array
    if (level == count.size())
        count.push_back(0);

    // Increase count of nodes at current level
    count[level]++;

    // Traverse left subtree
    preorder(root->left, level + 1, count);

    // Traverse right subtree
    preorder(root->right, level + 1, count);
}

// Returns the maximum width of the binary tree
int maxWidth(Node* root) {

    vector<int> count;

    // Store number of nodes at each level
    preorder(root, 0, count);

    int ans = 0;

    // Find maximum count among all levels
    for (int x : count) {
        ans = max(ans, x);
    }

    return ans;
}

int main() {

    // root = [1, 2, 3, 4, 5, 6, 7]
    //
    // Construct the following binary tree:
    //         1
    //       /   \
    //      2     3
    //     / \   / \
    //    4   5 6   7

    Node* root = new Node(1);

    root->left = new Node(2);
    root->right = new Node(3);

    root->left->left = new Node(4);
    root->left->right = new Node(5);

    root->right->left = new Node(6);
    root->right->right = new Node(7);

    cout << maxWidth(root);

    return 0;
}
Java
import java.util.ArrayList;

// Structure of a binary tree node
class Node {
    int data;
    Node left;
    Node right;

    Node(int x) {
        data = x;
        left = null;
        right = null;
    }
}

class GFG {

    // Preorder traversal to count nodes at each level
     public static void preorder(Node root, int level, ArrayList<Integer> count) {

        if (root == null)
            return;

        // Add new level in count array
        if (level == count.size())
            count.add(0);

        // Increase count of nodes at current level
        count.set(level, count.get(level) + 1);

        // Traverse left subtree
        preorder(root.left, level + 1, count);

        // Traverse right subtree
        preorder(root.right, level + 1, count);
    }

    // Returns the maximum width of the binary tree
     public static int maxWidth(Node root) {

        ArrayList<Integer> count = new ArrayList<>();

        // Store number of nodes at each level
        preorder(root, 0, count);

        int ans = 0;

        // Find maximum count among all levels
        for (int x : count) {
            ans = Math.max(ans, x);
        }

        return ans;
    }

    public static void main(String[] args) {

        // root = [1, 2, 3, 4, 5, 6, 7]
        //
        // Construct the following binary tree:
        //         1
        //       /   \
        //      2     3
        //     / \   / \
        //    4   5 6   7

        Node root = new Node(1);

        root.left = new Node(2);
        root.right = new Node(3);

        root.left.left = new Node(4);
        root.left.right = new Node(5);

        root.right.left = new Node(6);
        root.right.right = new Node(7);

        System.out.println(maxWidth(root));
    }
}
Python
# Structure of a binary tree node
class Node:

    def __init__(self, x):
        self.data = x
        self.left = None
        self.right = None


# Preorder traversal to count nodes at each level
def preorder(root, level, count):

    if root == None:
        return

    # Add new level in count array
    if level == len(count):
        count.append(0)

    # Increase count of nodes at current level
    count[level] += 1

    # Traverse left subtree
    preorder(root.left, level + 1, count)

    # Traverse right subtree
    preorder(root.right, level + 1, count)


# Returns the maximum width of the binary tree
def maxWidth(root):

    count = []

    # Store number of nodes at each level
    preorder(root, 0, count)

    ans = 0

    # Find maximum count among all levels
    for x in count:
        ans = max(ans, x)

    return ans


if __name__ == "__main__":

    # root = [1, 2, 3, 4, 5, 6, 7]
    #
    # Construct the following binary tree:
    #         1
    #       /   \
    #      2     3
    #     / \   / \
    #    4   5 6   7

    root = Node(1)

    root.left = Node(2)
    root.right = Node(3)

    root.left.left = Node(4)
    root.left.right = Node(5)

    root.right.left = Node(6)
    root.right.right = Node(7)

    print(maxWidth(root))
C#
using System;
using System.Collections.Generic;

// Structure of a binary tree node
class Node {
    public int data;
    public Node left;
    public Node right;

    public Node(int x) {
        data = x;
        left = null;
        right = null;
    }
}

class GFG {

    // Preorder traversal to count nodes at each level
      public static void preorder(Node root, int level, List<int> count) {

        if (root == null)
            return;

        // Add new level in count array
        if (level == count.Count)
            count.Add(0);

        // Increase count of nodes at current level
        count[level]++;

        // Traverse left subtree
        preorder(root.left, level + 1, count);

        // Traverse right subtree
        preorder(root.right, level + 1, count);
    }

    // Returns the maximum width of the binary tree
      public static int maxWidth(Node root) {

        List<int> count = new List<int>();

        // Store number of nodes at each level
        preorder(root, 0, count);

        int ans = 0;

        // Find maximum count among all levels
        foreach (int x in count) {
            ans = Math.Max(ans, x);
        }

        return ans;
    }

    public static void Main() {

        // root = [1, 2, 3, 4, 5, 6, 7]
        //
        // Construct the following binary tree:
        //         1
        //       /   \
        //      2     3
        //     / \   / \
        //    4   5 6   7

        Node root = new Node(1);

        root.left = new Node(2);
        root.right = new Node(3);

        root.left.left = new Node(4);
        root.left.right = new Node(5);

        root.right.left = new Node(6);
        root.right.right = new Node(7);

        Console.WriteLine(maxWidth(root));
    }
}
JavaScript
// Structure of a binary tree node
class Node {
    constructor(x) {
        this.data = x;
        this.left = null;
        this.right = null;
    }
}

// Preorder traversal to count nodes at each level
function preorder(root, level, count) {

    if (root == null)
        return;

    // Add new level in count array
    if (level == count.length)
        count.push(0);

    // Increase count of nodes at current level
    count[level]++;

    // Traverse left subtree
    preorder(root.left, level + 1, count);

    // Traverse right subtree
    preorder(root.right, level + 1, count);
}

// Returns the maximum width of the binary tree
function maxWidth(root) {

    let count = [];

    // Store number of nodes at each level
    preorder(root, 0, count);

    let ans = 0;

    // Find maximum count among all levels
    for (let x of count) {
        ans = Math.max(ans, x);
    }

    return ans;
}

// Driver code

// root = [1, 2, 3, 4, 5, 6, 7]
// Construct the following binary tree:
//         1
//       /   \
//      2     3
//     / \   / \
//    4   5 6   7

let root = new Node(1);

root.left = new Node(2);
root.right = new Node(3);

root.left.left = new Node(4);
root.left.right = new Node(5);

root.right.left = new Node(6);
root.right.right = new Node(7);

console.log(maxWidth(root));

Output
4
Comment