Skip to content

Commit 8c44f41

Browse files
add 1302
1 parent b096d9c commit 8c44f41

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1302|[Deepest Leaves Sum](https://leetcode.com/problems/deepest-leaves-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1302.java) | |Medium||
1112
|1297|[Maximum Number of Occurrences of a Substring](https://leetcode.com/problems/maximum-number-of-occurrences-of-a-substring/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1297.java) | |Medium||
1213
|1296|[Divide Array in Sets of K Consecutive Numbers](https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1296.java) | |Medium||
1314
|1295|[Find Numbers with Even Number of Digits](https://leetcode.com/problems/find-numbers-with-even-number-of-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1295.java) | |Easy||
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
8+
/**
9+
* 1302. Deepest Leaves Sum
10+
*
11+
* Given a binary tree, return the sum of values of its deepest leaves.
12+
*
13+
* Example 1:
14+
* 1
15+
* / \
16+
* 2 3
17+
* / \ \
18+
* 4 5 6
19+
* / \
20+
* 7 8
21+
*
22+
* Input: root = [1,2,3,4,5,null,6,7,null,null,null,null,8]
23+
* Output: 15
24+
*
25+
* Constraints:
26+
* The number of nodes in the tree is between 1 and 10^4.
27+
* The value of nodes is between 1 and 100.
28+
* */
29+
public class _1302 {
30+
public static class Solution1 {
31+
public int deepestLeavesSum(TreeNode root) {
32+
int depth = maxDepth(root);
33+
return bfs(root, depth);
34+
}
35+
36+
private int bfs(TreeNode root, int depth) {
37+
Queue<TreeNode> queue = new LinkedList<>();
38+
queue.offer(root);
39+
int currentLevel = 0;
40+
int sum = 0;
41+
while (!queue.isEmpty()) {
42+
int size = queue.size();
43+
currentLevel++;
44+
for (int i = 0; i < size; i++) {
45+
TreeNode currNode = queue.poll();
46+
if (currentLevel == depth) {
47+
sum += currNode.val;
48+
}
49+
if (currNode.left != null) {
50+
queue.offer(currNode.left);
51+
}
52+
if (currNode.right != null) {
53+
queue.offer(currNode.right);
54+
}
55+
}
56+
}
57+
return sum;
58+
}
59+
60+
private int maxDepth(TreeNode root) {
61+
if (root == null) {
62+
return 0;
63+
}
64+
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
65+
}
66+
}
67+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.TreeUtils;
4+
import com.fishercoder.solutions._1302;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import java.util.Arrays;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class _1302Test {
13+
private static _1302.Solution1 solution1;
14+
15+
@BeforeClass
16+
public static void setup() {
17+
solution1 = new _1302.Solution1();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
assertEquals(15, solution1.deepestLeavesSum(TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, null, 6, 7, null, null, null, null, 8))));
23+
}
24+
25+
}

0 commit comments

Comments
 (0)