-
Notifications
You must be signed in to change notification settings - Fork 0
/
5a.java
28 lines (23 loc) · 804 Bytes
/
5a.java
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
26
27
28
class Solution {
public int deepestLeavesSum(TreeNode root) {
if (root == null) return 0;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int deepestSum = 0;
while (!queue.isEmpty()) {
int levelSize = queue.size();
deepestSum = 0; // Reset sum at the start of each level
for (int i = 0; i < levelSize; i++) {
TreeNode currentNode = queue.poll();
deepestSum += currentNode.val;
if (currentNode.left != null) {
queue.offer(currentNode.left);
}
if (currentNode.right != null) {
queue.offer(currentNode.right);
}
}
}
return deepestSum;
}
}