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
Example 1:1
2
3
4
5
6
7
8
9
10Input:
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:1
2
3
4
5
6
7
8
9
10Input:
1
/
3
/ \
5 3
Output: 2
Explanation: The maximum width existing in the third level with the length 2 (5,3).
Example 3:1
2
3
4
5
6
7
8
9
10Input:
1
/ \
3 2
/
5
Output: 2
Explanation: The maximum width existing in the second level with the length 2 (3,2).
Example 4:1
2
3
4
5
6
7
8
9
10
11Input:
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).
Solution
Solution 1: Best solution
Used list to record the start index of current level. The index of root is 1. The left child and right can be represented as 2 i and 2 i + 1. So the width of one level is the index of right most node - left most node + 1.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private int max = 1;
public int widthOfBinaryTree(TreeNode root) {
if (root == null) return 0;
List<Integer> startOfLevel = new LinkedList<>();
helper(root, 0, 1, startOfLevel);
return max;
}
public void helper(TreeNode root, int level, int index, List<Integer> list) {
if (root == null) return;
if (level == list.size()) list.add(index);
max = Math.max(max, index + 1 - list.get(level));
helper(root.left, level + 1, index * 2, list);
helper(root.right, level + 1, index * 2 + 1, list);
}
}
Solution: Not good enough solution1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38/**
* 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) {
if (root == null) return 0;
int res = 1;
List<TreeNode> level = new LinkedList<>();
level.add(root);
while(level.size() > 0){
int size = level.size();
for (int i = 0; i < size; i++){
TreeNode tmp = level.remove(0);
if (tmp != null){
level.add(tmp.left);
level.add(tmp.right);
}
else{
level.add(null);
level.add(null);
}
}
while(level.size() > 0 && level.get(0) == null) level.remove(0);
while(level.size() > 0 && level.get(level.size() - 1) == null) level.remove(level.size() - 1);
res = Math.max(res, level.size());
}
return res;
}
}