Maximum Width of Binary Tree

update Sep 16 2018, 16:28

LeetCodearrow-up-right

Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as a full binary tree, but some nodes are null.

The width of one level is defined as the length between the end-nodes (the leftmost and right most non-null nodes in the level, where the null nodes between the end-nodes are also counted into the length calculation.

Example 1:

Input:

       1
     /   \
    3     2
   / \     \  
  5   3     9

Output: 4 Explanation: The maximum width existing in the third level with the length 4 (5,3,null,9).

Example 2:

Input:

      1
     /  
    3    
   / \       
  5   3     

Output: 2 Explanation: The maximum width existing in the third level with the length 2 (5,3).

Example 3:

Input:

Output: 2 Explanation: The maximum width existing in the second level with the length 2 (3,2).

Example 4:

Input:

Output: 8 Explanation:The maximum width existing in the fourth level with the length 8 (6,null,null,null,null,null,null,7).

Note: Answer will in the range of 32-bit signed integer.

Basic Idea:

求给定二叉树的最大宽度,最大宽度的两边必须在同一层中,最大宽度的定义为当前层如果填满leftMost和rightMost之间的Node,当前层node的个数。

可以利用数组存heap的思路,将node和index对应起来。利用queue进行层序遍历,每次和node一同enque的还有该node的index (left child 为 index * 2,right 为 index * 2 + 1)。每层考虑最左和最右index的差值+1,返回值就是这些值之中最大的。

Java Code: