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:
- Find height of tree
Set MaxWidth = 0
- Iterate height from 0 to height
- Find width for each height
- If current width > MaxWidth then
- >MaxWidth = current width
Return MaxWidth
Latest Source Code:
Github: BinaryTreeMaxWidthFinder.java
Output:
6 / \ / \ / \ / \ 13 8 / \ / \ / \ / \ 1 5 7 11 / \ 9 3 Width : 4