Update appNew update is available. Click here to update.
Topics

Maximum Width In Binary Tree

Moderate
0/80
Average time to solve is 38m
profile
Contributed by
41 upvotes
GE (General Electric)FlipkartAmazon
+3 more companies

Problem statement

You have been given a Binary Tree of integers. You are supposed to return the maximum width of the given Binary Tree. The maximum width of the tree is the maximum width among all the levels of the given tree.


The width of one level is defined as the length between the leftmost and the rightmost, non-null nodes in the level, where the null nodes in between the leftmost and rightmost are excluded into length calculation.


For example :
For the given binary tree

Example

The maximum width will be at the third level with the length of 3, i.e. {4, 5, 6}.
Detailed explanation ( Input/output format, Notes, Images )
Sample Input 1 :
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Sample Output 1 :
3
Explanation of Sample Input 1 :

Example The maximum width will be at the third level with the length of 3, i.e. {4, 5, 6}.

Sample Input 2 :
2 7 5 2 6 -1 9 -1 -1 5 11 4 -1 -1 -1 -1 -1 -1 -1
Sample Output 2 :
3
Constraints :
0 <= 'N' <= 5 * 10 ^ 5
0 <= 'DATA' <= 10 ^ 6 and data != -1
Where ‘N’ is the total number of nodes in the binary tree, and 'DATA' is the value of the binary tree node.

Time Limit: 1sec.
Full screen
Console