Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Find the maximum width of a binary tree

Objec­tive: Given a binary Tree, write an algo­rithm to find the max­i­mum width.

Note: Max­i­mum width of a tree is noth­ing but the Max(nodes at each level).

Input: A Binary Tree

Out­put: Max­i­mum width of a given tree.

Exam­ple:

Maximum width of a given tree

Max­i­mum width of a given tree

Approach:

The solu­tion of this prob­lem is very sim­ple, Do level order tra­ver­sal and in recur­sive calls count the num­ber of nodes at each level and keep track of Max of them and at the end return the max.

Read this solu­tion ” Level Order Tra­ver­sal, Print each level in sep­a­rate line” and imple­ment the above approach.

Com­plete Code:


Out­put:

Maximum Width of a binary Tree is : 4

You may also like...