Maximum Width of Tree | Practice | GeeksforGeeks

Given a Binary Tree, find the maximum width of it. Maximum width is defined as the maximum number of nodes in any level.
For example, maximum width of following tree is 4 as there are 4 nodes at 3rd level.
          1
       /     \
     2        3
   /    \    /    \
  4    5   6    7
    \
      8
Example 1:
Input:
       1
     /    \
    2      3
Output: 2

Example 2:
Input:
        10
      /     \
    20      30
   /    \
  40    60
Output: 2

Your Task:
You don't have to read any input. Just complete the function getMaxWidth() that takes node as parameter and returns the maximum width. The printing is done by the driver code.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= Number of Nodes<= 105
0 <= nodes values <= 105
 
+1
amishasahu3283 weeks ago
class Solution {
  public:
    // Function to get the maximum width of a binary tree.
    int getMaxWidth(Node* root) {

        // Your code here
        if(!root) return 0;
        queue<Node*> q;
        q.push(root);
        int ans = 0;
        while(!q.empty())
        {
            int size = q.size();
            ans = max(ans, size);
            for(int i = 0; i < size; i++)
            {
                Node *node = q.front();
                q.pop();
                if(node->left)
                    q.push(node->left);
                if(node->right)
                    q.push(node->right);
            }
            
        }
        return ans;
    }
};
+1
geniusambi881 month ago
Simple C++ Solution
 
int getMaxWidth(Node* root) {
       if(root==NULL)       {           return 0;       }       int res=0;       queue<Node *>q;       q.push(root);       while(!q.empty())       {           int count=q.size();           res=max(res,count);           for(int i=0;i<count;i++)           {           Node *curr=q.front();           q.pop();           if(curr->left!=NULL)           {               q.push(curr->left);           }           if(curr->right!=NULL)           {               q.push(curr->right);           }                          }       }       return res;   }
0
amanavengeraman1 month ago
      if(root == null)return 0;              int maxwid = Integer.MIN_VALUE;       Queue<Node> q = new LinkedList<>();       q.offer(root);              while(!q.isEmpty()){                      int size = q.size();           maxwid = Math.max(maxwid, size);                      for(int i=0; i<size; i++){               Node temp = q.poll();               if(temp.left != null){                   q.offer(temp.left);               }               if(temp.right != null){                   q.offer(temp.right);               }           }       }       return maxwid;
0
detroix071 month ago
int getMaxWidth(Node* root) {       queue<Node*> q;   q.push(root);   q.push(NULL);   int max = q.size()-1;       while(q.size() != 0) {    Node* Front = q.front();    q.pop();    if(Front==NULL) {     if(q.size() > max) {       max=q.size();     }     if(q.size()==0) break;     q.push(NULL);    } else {     if(Front->left) q.push(Front->left);     if(Front->right) q.push(Front->right);    }       }      return max;          }
0
rampurnaveen91 month ago
int getMaxWidth(Node* root)    {       queue<Node*>q;       q.push(root);       int k=0;       while(!q.empty())       {           int size = q.size();           for(int i=0;i<size;i++)           {               Node *temp = q.front();               q.pop();               if(temp->left)                   q.push(temp->left);               if(temp->right)                   q.push(temp->right);           }           k = max(k,size);       }       return k;       // Your code here   }
0
abhudaymishra298
This comment was deleted.
+1
sarthakjagetiya10011 month ago
Total Time Taken:  0.1/1.1
C++
int getMaxWidth(Node* root) {
        queue<Node*> q;
        q.push(root);
        q.push(NULL);
        int width=1;
        while(!q.empty()){
          Node* node = q.front();
          q.pop();
          if(node!=NULL){
            if(node->left)
              q.push(node->left);
            if(node->right)
              q.push(node->right);
          }
          else if(!q.empty()){
            int n=q.size();
            width=max(width, n);
            q.push(NULL);
          }
        }
        return width;
    }
0
codedaddy422 months ago
class Solution {
  public:
    // Function to get the maximum width of a binary tree.
    int getMaxWidth(Node* root) {
        queue<Node*>q;
        q.push(root);
        int res=0;
        while(!q.empty())
        {
            int count=q.size();
            res=max(res,count);
            //entering the child nodes of every node present in the Q
            for(int i=0;i<count;i++)
            {
                Node* curr=q.front();
                q.pop();
                if(curr->left!=NULL)
                q.push(curr->left);
                if(curr->right!=NULL)
                q.push(curr->right);
            }
        }
        return res;
    }
};
0
dronzerdracel2 months ago
My simple recursive solution
class Solution {
  public:
    // Function to get the maximum width of a binary tree.
    int maxm=0;
    unordered_map<int,int>mp;
    void width(Node* root,int level){
        if(!root)
         return;
        mp[level]++;
        maxm=max(maxm,mp[level]);
        width(root->left,level+1);
        width(root->right,level+1);
    }
    int getMaxWidth(Node* root) {
        // Your code here
        width(root,0);
        return maxm;
    }
};
0
jumbojoy2k2 months ago
class Solution {
  public:
    // Function to get the maximum width of a binary tree.
    int getMaxWidth(Node* root) {
        if(root==NULL) return 0;
        if(root->left==NULL and root->right==NULL) return 1;

        // Your code here
        queue<Node*> q;
        q.push(root);
        q.push(NULL);
        int c=0,ans=INT_MIN;
        while(!q.empty())
        {
            Node *f=q.front();
            q.pop();
            if(f==NULL)
            {
                ans=max(c,ans);
                c=0;
                if(!q.empty()) q.push(NULL);
            }
            else
            {
                if(f->left) c++,q.push(f->left);
                if(f->right) c++,q.push(f->right);
            }
        }
        return ans;
    }
};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.