# 662.Maximum-Width-of-Binary-Tree

## 662. Maximum Width of Binary Tree

## 题目地址

[https://leetcode.com/problems/maximum-width-of-binary-tree/](https://leetcode.com/problems/maximum-width-of-binary-tree/)

## 题目描述

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

## 代码

### Approach #1 DFS

Time Complexity & Space Complexity: O(N)

```java
class Solution {
  int ans;
  Map<Integer, Integer> left;
  public int widthOfBinaryTree(TreeNode root) {
    ans = 0;
    left = new HashMap();
    dfs(root, 0, 0);
    return ans;
  }

  public void dfs(TreeNode root, int depth, int pos) {
    if (root == null) return;
    left.putIfAbsent(depth, pos);
    ans = Math.max(ans, pos - left.get(depth) + 1);
    dfs(root.left, depth + 1, 2 * pos);
    dfs(root.right, depth + 1, 2 * pos + 1);
  }
}
```

### Approach #2 BFS

Time Complexity & Space Complexity: O(N)

```java
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int widthOfBinaryTree(TreeNode root) {
            Queue<AnnotateNode> queue = new LinkedList();
      queue.add(new AnnotateNode(root, 0, 0));
      int ans = 0;
      int curDepth = 0; 
      int left = 0;
      while (!queue.isEmpty()) {
        AnnotateNode a = queue.poll();
        if (a.node != null) {
          queue.add(new AnnotateNode(a.node.left, a.depth + 1, a.pos * 2));
          queue.add(new AnnotateNode(a.node.right, a.depth + 1, a.pos * 2 + 1));
          if (curDepth != a.depth) {
            curDepth = a.depth;
            left = a.pos;
          }
          ans = Math.max(ans, a.pos - left + 1);
        }
      }

      return ans;
    }
}

class AnnotateNode {
  TreeNode node;
  int depth;
  int pos;
  AnnotateNode(TreeNode, n, int d, int p) {
    node = n;
    depth = d;
    pos = p;
  }
}
```

### Approach #3 Queue

```java
class Solution {
    public int widthOfBinaryTree(TreeNode root) {
        if (root == null) return 0;
        Queue<TreeNode> q = new LinkedList<TreeNode>();
        Map<TreeNode, Integer> m = new HashMap<TreeNode, Integer>();
        q.offer(root);
        m.put(root, 1);
        int curW = 0;
        int maxW = 0;
        while (!q.isEmpty()) {
            int size = q.size();
            int start = 0;
            int end = 0;
            for (int i = 0; i < size; i++) {
                TreeNode node = q.poll();
                if (i == 0) start = m.get(node);
                if (i == size - 1) end = m.get(node);
                if (node.left != null) {
                    m.put(node.left, m.get(node) * 2);
                    q.offer(node.left);
                }
                if (node.right != null) {
                    m.put(node.right, m.get(node) * 2 + 1);
                    q.offer(node.right);
                }
            }
            curW = end - start + 1;
            maxW = Math.max(curW, maxW);
        }
        return maxW;
    }
}
```
