662-Maximum Width of Binary Tree

kevin聰發表於2018-05-25

Description

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: 

          1
         / \
        3   2 
       /        
      5      

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

Example 4:

Input: 

          1
         / \
        3   2
       /     \  
      5       9 
     /         \
    6           7
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.


問題描述

給定一個二叉樹, 返回其最大寬度。一個樹的寬度為其所有層級中的最大寬度。給定的二叉樹與滿二叉樹結構相同, 但是其中的一些節點為空節點。

一個層級的寬度被定義為該層中的端點之間的距離(最左邊及最右邊的非空節點, 端點間的空節點也要包括在距離計算中)


問題分析

關鍵是為每層的節點標記序號, 序號的產生規則為左分支為2i, 右分支為2i + 1, i初始化為1

遞迴的解法注意利用高度


解法1

class Solution {
    private int max = 0;

    public int widthOfBinaryTree(TreeNode root) {
        List<Integer> startIndexOfEachLevel = new ArrayList<>();
        helper(root, 1, 1, startIndexOfEachLevel);
        return max;
    }

    public void helper(TreeNode node, int level, int index, List<Integer> startIndexOfEachLevel) {
        if(node == null)    return;

        if(level > startIndexOfEachLevel.size())    startIndexOfEachLevel.add(index);            

        max = Math.max(max, index - startIndexOfEachLevel.get(level - 1) + 1);
        helper(node.left, level + 1, index * 2, startIndexOfEachLevel);
        helper(node.right, level + 1, index * 2 + 1, startIndexOfEachLevel);
    }
}

解法2

class Solution {
    private int max = 0;

    public int widthOfBinaryTree(TreeNode root) {
        List<Integer> startIndexOfEachLevel = new ArrayList<>();
        helper(root, 1, 1, startIndexOfEachLevel);
        return max;
    }

    public void helper(TreeNode node, int level, int index, List<Integer> startIndexOfEachLevel) {
        if(node == null)    return;

        if(level > startIndexOfEachLevel.size())    startIndexOfEachLevel.add(index);            

        max = Math.max(max, index - startIndexOfEachLevel.get(level - 1) + 1);
        helper(node.left, level + 1, index * 2, startIndexOfEachLevel);
        helper(node.right, level + 1, index * 2 + 1, startIndexOfEachLevel);
    }
}

相關文章