1. 程式人生 > >662. Maximum Width of Binary Tree二叉樹的最大寬度

662. Maximum Width of Binary Tree二叉樹的最大寬度

hide play evel view define font defined nodes 如果

[抄題]:

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).

[暴力解法]:

時間分析:

空間分析:

[優化後]:

時間分析:

空間分析:

[奇葩輸出條件]:

[奇葩corner case]:

從第三層開始,如果已經滿了,就要添加新的index

if (level == list.size()) list.add(index);

[思維問題]:

完全沒思路,因此需要一些基礎知識

[英文數據結構或算法,為什麽不用別的數據結構或算法]:

We know that a binary tree can be represented by an array (assume the root begins from the position with index 1 in the array). If the index of a node is i, the indices of its two children are 2*i and 2*i + 1. The idea is to use two arrays (start[] and end[]) to record the the indices of the leftmost node and rightmost node in each level, respectively. For each level of the tree, the width isend[level] - start[level] + 1. Then, we just need to find the maximum width.

[一句話思路]:

[輸入量]:空: 正常情況:特大:特小:程序裏處理到的特殊情況:異常情況(不合法不合理的輸入):

[畫圖]:

[一刷]:

index的初始值為啥是1?不懂,算了

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分鐘肉眼debug的結果]:

[總結]:

找參數只是結果,重要的是把所需的變量找出來

還是按照起點、過程、終點來寫,index的左右分別為 2 * index 和 2 * index + 1,

[復雜度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:叠代/遞歸/分治/貪心]:

[關鍵模板化代碼]:

[其他解法]:

[Follow Up]:

[LC給出的題目變變變]:

[代碼風格] :

[是否頭一次寫此類driver funcion的代碼] :

[潛臺詞] :

技術分享圖片
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    int max = 1;
    
    public int widthOfBinaryTree(TreeNode root) {
        //corner case
        if (root == null) return 0;
        
        //initialization
        List<Integer> startOfLevel = new ArrayList<Integer>();
        
        //return
        getWidth(root, 1, 0, startOfLevel);
        return max;
    }
    
    public void getWidth(TreeNode root, int index, int level, List<Integer> list) {
        //return null
        if (root == null) return ;
        
        //add the index to list
        if (list.size() == level)
            list.add(index);
        
        max = Math.max(max, index + 1 - list.get(level));
        
        //divide and conquer in left and right
        getWidth(root.left, 2 * index, level + 1, list);
        getWidth(root.right, 2 * index + 1, level + 1, list);
    }
}
View Code

662. Maximum Width of Binary Tree二叉樹的最大寬度