Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Maximum Width In Binary Tree
MEDIUM
38 mins
20 upvotes
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Maximum Width In Binary Tree

Contributed by
Mutiur Rehman khan
Medium
yellow-spark
0/80
Avg time to solve 38 mins
Success Rate 50 %
Share
20 upvotes

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, Constraints, 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 :
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
Explanation of Sample Input 2 :
The maximum width will be at the third level i.e. {2, 6, 9} and the fourth level with the length of 3, i.e. {5, 11, 4}. So the maximum width will be 3.
Reset Code
Full screen
Auto
copy-code
Console