Skip to content

Commit dc29c0f

Browse files
add a solution for 1302
1 parent 25ba7da commit dc29c0f

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/main/java/com/fishercoder/solutions/_1302.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,27 @@ private int maxDepth(TreeNode root) {
4343
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
4444
}
4545
}
46+
47+
public static class Solution2 {
48+
public int deepestLeavesSum(TreeNode root) {
49+
Queue<TreeNode> queue = new LinkedList<>();
50+
queue.offer(root);
51+
int sum = 0;
52+
while (!queue.isEmpty()) {
53+
int size = queue.size();
54+
sum = 0;
55+
for (int i = 0; i < size; i++) {
56+
TreeNode curr = queue.poll();
57+
sum += curr.val;
58+
if (curr.left != null) {
59+
queue.offer(curr.left);
60+
}
61+
if (curr.right != null) {
62+
queue.offer(curr.right);
63+
}
64+
}
65+
}
66+
return sum;
67+
}
68+
}
4669
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy