Find maximum width of a binary tree

Maximum width of a binary tree

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

Solution:

  • There are two solution
  • Either use queue for order level traversal and return max width
  • Or, find height and for each find width
Algorithm for second approach:
  1. Find height of tree
  2. Set MaxWidth = 0
  3. Iterate height from 0 to height
    1. Find width for each height
    2. If current width > MaxWidth then
        >MaxWidth = current width
  4. Return MaxWidth

Latest Source Code:
Github: BinaryTreeMaxWidthFinder.java


Output:

       6               
      / \       
     /   \      
    /     \     
   /       \    
   13       8       
  / \     / \   
 /   \   /   \  
 1   5   7   11   
            / \ 
            9 3 
                                
Width : 4
Author: Hrishikesh Mishra