Maximum Width of Binary Tree - Codeprg

Breaking

programing News Travel Computer Engineering Science Blogging Earning

Friday 21 August 2020

Maximum Width of Binary Tree

Maximum Width of Binary Tree

 Maximum Width of Binary Tree

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

The width of one level is defined as the length between the end-nodes (the leftmost and right most non-null nodes in the level, where the null nodes between the end-nodes are also counted into the length calculation.

It is guaranteed that the answer will in the range of 32-bit signed integer.

Example 1:

Input: 

           1
         /   \
        3     2
       / \     \  
      5   3     9 

Output: 4
Explanation: The maximum width existing in the third level with the length 4 (5,3,null,9).

Example 2:

Input: 

          1
         /  
        3    
       / \       
      5   3     

Output: 2
Explanation: The maximum width existing in the third level with the length 2 (5,3).

Example 3:

Input: 

          1
         / \
        3   2 
       /        
      5      

Output: 2
Explanation: The maximum width existing in the second level with the length 2 (3,2).

Example 4:

Input: 

          1
         / \
        3   2
       /     \  
      5       9 
     /         \
    6           7
Output: 8
Explanation:The maximum width existing in the fourth level with the length 8 (6,null,null,null,null,null,null,7).

 

Constraints:

  • The given binary tree will have between 1 and 3000 nodes.

 /**

 * Definition for a binary tree node.

 * struct TreeNode {

 *     int val;

 *     TreeNode *left;

 *     TreeNode *right;

 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}

 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}

 * };

 */

class Solution {

private:

    // long long int max_width;

    // unordered_map<long long int,long long int> mp;

public:

    // void widthOfBinary(TreeNode* root,long long int depth,long long int pos)

    // {

    //     if(root==NULL)

    //         return ;

    //     if(mp.find(depth)==mp.end())

    //         mp[depth]=pos;

    //     if(max_width<pos-mp[depth]+1)

    //       max_width=pos-mp[depth]+1;

    //     widthOfBinary(root->left,depth+1,2*pos);

    //     widthOfBinary(root->right,depth+1,2*pos+1);

    // }

    

    int widthOfBinaryTree(TreeNode* root) 

    {

        if(root==NULL)

            return 0;

        

        int  max_width=1;

        queue<pair<TreeNode*,int>> q;

        q.push({root,0});

        while(!q.empty())

        {

            int l=q.size(),s=0,e=0;

              s=q.front().second;

              e=q.back().second;

             // cout<<e<<" "<<s<<" "<<max_width<<endl;

              max_width=max(max_width,e-s+1);

            while(l--)

            {

                auto p=q.front();

                   q.pop();

                long int k=p.second-s;

                // cout<<k<<endl;

                if(p.first->left)

                    q.push({p.first->left,2*(k)+1});

                if(p.first->right)

                    q.push({p.first->right,2*(k)+2});

            }

        }

        return max_width;

    }

    

};