Skip to content

Commit 8e97182

Browse files
add 662
1 parent 7b8fa7d commit 8e97182

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Your ideas/fixes/algorithms are more than welcome!
2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
2525
|663|[Equal Tree Partition](https://leetcode.com/problems/equal-tree-partition/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_663.java) | O(n) | O(n) | Medium | Tree
26+
|662|[Maximum Width of Binary Tree](https://leetcode.com/problems/maximum-width-of-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_662.java) | O(n) | O(k) | Medium | BFS, DFS
2627
|661|[Image Smoother](https://leetcode.com/problems/image-smoother/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_661.java) | O(m*n) | O(1) | Easy | Array
2728
|660|[Remove 9](https://leetcode.com/problems/remove-9/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_660.java) | O(n) | O(1) | Hard | Math
2829
|659|[Split Array into Consecutive Subsequences](https://leetcode.com/problems/split-array-into-consecutive-subsequences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_659.java) | O(n) | O(n) | Medium | HashMap
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
import java.util.*;
6+
7+
/**
8+
* 662. Maximum Width of Binary Tree
9+
*
10+
* Given a binary tree, write a function to get the maximum width of the given tree.
11+
* The width of a tree is the maximum width among all levels.
12+
* The binary tree has the same structure as a full binary tree, but some nodes are null.
13+
* The width of one level is defined as the length between the end-nodes
14+
* (the leftmost and right most non-null nodes in the level,
15+
* where the null nodes between the end-nodes are also counted into the length calculation.
16+
17+
Example 1:
18+
Input:
19+
20+
1
21+
/ \
22+
3 2
23+
/ \ \
24+
5 3 9
25+
26+
Output: 4
27+
Explanation: The maximum width existing in the third level with the length 4 (5,3,null,9).
28+
29+
Example 2:
30+
Input:
31+
32+
1
33+
/
34+
3
35+
/ \
36+
5 3
37+
38+
Output: 2
39+
Explanation: The maximum width existing in the third level with the length 2 (5,3).
40+
41+
Example 3:
42+
Input:
43+
44+
1
45+
/ \
46+
3 2
47+
/
48+
5
49+
50+
Output: 2
51+
Explanation: The maximum width existing in the second level with the length 2 (3,2).
52+
Example 4:
53+
Input:
54+
55+
1
56+
/ \
57+
3 2
58+
/ \
59+
5 9
60+
/ \
61+
6 7
62+
63+
Output: 8
64+
Explanation:The maximum width existing in the fourth level with the length 8 (6,null,null,null,null,null,null,7).
65+
66+
Note: Answer will in the range of 32-bit signed integer.
67+
*/
68+
public class _662 {
69+
public int widthOfBinaryTree(TreeNode root) {
70+
if (root == null) {
71+
return 0;
72+
}
73+
Queue<Map.Entry<TreeNode, Integer>> queue = new LinkedList<>();
74+
queue.offer(new AbstractMap.SimpleEntry<>(root, 1));
75+
int max = 1;
76+
while (!queue.isEmpty()) {
77+
int size = queue.size();
78+
List<Map.Entry<TreeNode, Integer>> list = new ArrayList<>();
79+
for (int i = 0; i < size; i++) {
80+
Map.Entry<TreeNode, Integer> curr = queue.poll();
81+
if (curr.getKey().left != null) {
82+
Map.Entry<TreeNode, Integer> newEntry = new AbstractMap.SimpleEntry<>(curr.getKey().left, curr.getValue() * 2 - 1);
83+
queue.offer(newEntry);
84+
list.add(newEntry);
85+
}
86+
if (curr.getKey().right != null) {
87+
Map.Entry<TreeNode, Integer> newEntry = new AbstractMap.SimpleEntry<>(curr.getKey().right, curr.getValue() * 2);
88+
queue.offer(newEntry);
89+
list.add(newEntry);
90+
}
91+
}
92+
if (list.size() > 1) {
93+
max = Math.max(list.get(list.size() - 1).getValue() - list.get(0).getValue() + 1, max);
94+
}
95+
}
96+
return max;
97+
}
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._662;
6+
import org.junit.BeforeClass;
7+
import org.junit.Ignore;
8+
import org.junit.Test;
9+
10+
import java.util.Arrays;
11+
12+
import static junit.framework.TestCase.assertEquals;
13+
14+
public class _662Test {
15+
private static _662 test;
16+
private static TreeNode root;
17+
private static int expected;
18+
19+
@BeforeClass
20+
public static void setup(){
21+
test = new _662();
22+
}
23+
24+
@Test
25+
public void test1(){
26+
root = TreeUtils.constructBinaryTree(Arrays.asList(1,3,2,5,3,null,9));
27+
expected = 4;
28+
assertEquals(expected, test.widthOfBinaryTree(root));
29+
}
30+
31+
@Test
32+
public void test2(){
33+
root = TreeUtils.constructBinaryTree(Arrays.asList(1,3,null, 5 ,3));
34+
expected = 2;
35+
assertEquals(expected, test.widthOfBinaryTree(root));
36+
}
37+
38+
@Test
39+
public void test3(){
40+
root = TreeUtils.constructBinaryTree(Arrays.asList(1,3,2,5));
41+
expected = 2;
42+
assertEquals(expected, test.widthOfBinaryTree(root));
43+
}
44+
45+
@Test
46+
@Ignore
47+
/**TODO: need to figure out how to pass in the input for the 4th example on Leetcode*/
48+
public void test4(){
49+
root = TreeUtils.constructBinaryTree(Arrays.asList(1,3,2,5,null,null,9,6,null,null,null,null,null,null,7));
50+
expected = 8;
51+
assertEquals(expected, test.widthOfBinaryTree(root));
52+
}
53+
54+
@Test
55+
public void test5(){
56+
root = TreeUtils.constructBinaryTree(Arrays.asList(1));
57+
expected = 1;
58+
assertEquals(expected, test.widthOfBinaryTree(root));
59+
}
60+
}

0 commit comments

Comments
 (0)