Skip to content

Commit 5f5052e

Browse files
add 1161
1 parent ae56486 commit 5f5052e

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ _If you like this project, please leave me a star._ ★
5959
|1176|[Diet Plan Performance](https://leetcode.com/problems/diet-plan-performance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1176.java) | |Easy|Array, Sliding Window|
6060
|1175|[Prime Arrangements](https://leetcode.com/problems/prime-arrangements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1175.java) | |Easy||
6161
|1165|[Single-Row Keyboard](https://leetcode.com/problems/single-row-keyboard/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1165.java) | |Easy||
62+
|1161|[Maximum Level Sum of a Binary Tree](https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1161.java) | |Medium|Graph|
6263
|1160|[Find Words That Can Be Formed by Characters](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1160.java)| |Easy||
6364
|1154|[Day of the Year](https://leetcode.com/problems/day-of-the-year/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1154.java) | |Easy||
6465
|1150|[Check If a Number Is Majority Element in a Sorted Array](https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1150.java)|[:tv:](https://youtu.be/-t2cdVs9cKk) |Easy||
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
import java.util.LinkedList;
6+
import java.util.Queue;
7+
import java.util.TreeMap;
8+
9+
/**
10+
* 1161. Maximum Level Sum of a Binary Tree
11+
*
12+
* Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on.
13+
* Return the smallest level X such that the sum of all the values of nodes at level X is maximal.
14+
*
15+
* Example 1:
16+
* 1
17+
* / \
18+
* 7 0
19+
* / \
20+
* 7 -8
21+
*
22+
* Input: [1,7,0,7,-8,null,null]
23+
* Output: 2
24+
*
25+
* Explanation:
26+
* Level 1 sum = 1.
27+
* Level 2 sum = 7 + 0 = 7.
28+
* Level 3 sum = 7 + -8 = -1.
29+
* So we return the level with the maximum sum which is level 2.
30+
*
31+
* Note:
32+
* The number of nodes in the given tree is between 1 and 10^4.
33+
* -10^5 <= node.val <= 10^5
34+
* */
35+
public class _1161 {
36+
public static class Solution1 {
37+
public int maxLevelSum(TreeNode root) {
38+
if (root == null) {
39+
return 0;
40+
}
41+
Queue<TreeNode> q = new LinkedList<>();
42+
q.offer(root);
43+
TreeMap<Integer, Integer> treeMap = new TreeMap<>((a, b) -> b - a);
44+
int level = 1;
45+
while (!q.isEmpty()) {
46+
int size = q.size();
47+
int sum = 0;
48+
for (int i = 0; i < size; i++) {
49+
TreeNode curr = q.poll();
50+
sum += curr.val;
51+
if (curr.left != null) {
52+
q.offer(curr.left);
53+
}
54+
if (curr.right != null) {
55+
q.offer(curr.right);
56+
}
57+
}
58+
treeMap.put(sum, level);
59+
level++;
60+
}
61+
return treeMap.firstEntry().getValue();
62+
}
63+
}
64+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._1161;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import java.util.Arrays;
10+
11+
import static junit.framework.Assert.assertEquals;
12+
13+
public class _1161Test {
14+
private static _1161.Solution1 solution1;
15+
16+
@BeforeClass
17+
public static void setup() {
18+
solution1 = new _1161.Solution1();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, 7, 0, 7, -8, null, null));
24+
assertEquals(2, solution1.maxLevelSum(root));
25+
}
26+
}

0 commit comments

Comments
 (0)