package com.fishercoder.solutions;

import com.fishercoder.common.classes.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

public class _1302 {
    public static class Solution1 {
        public int deepestLeavesSum(TreeNode root) {
            int depth = maxDepth(root);
            return bfs(root, depth);
        }

        private int bfs(TreeNode root, int depth) {
            Queue<TreeNode> queue = new LinkedList<>();
            queue.offer(root);
            int currentLevel = 0;
            int sum = 0;
            while (!queue.isEmpty()) {
                int size = queue.size();
                currentLevel++;
                for (int i = 0; i < size; i++) {
                    TreeNode currNode = queue.poll();
                    if (currentLevel == depth) {
                        sum += currNode.val;
                    }
                    if (currNode.left != null) {
                        queue.offer(currNode.left);
                    }
                    if (currNode.right != null) {
                        queue.offer(currNode.right);
                    }
                }
            }
            return sum;
        }

        private int maxDepth(TreeNode root) {
            if (root == null) {
                return 0;
            }
            return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
        }
    }
}

@@ -1383,11 +1383,758 @@
  %7D%0A    %7D
+%0A%0A    public static class Solution2 %7B%0A        public int deepestLeavesSum(TreeNode root) %7B%0A            Queue%3CTreeNode%3E queue = new LinkedList%3C%3E();%0A            queue.offer(root);%0A            int sum = 0;%0A            while (!queue.isEmpty()) %7B%0A                int size = queue.size();%0A                sum = 0;%0A                for (int i = 0; i %3C size; i++) %7B%0A                    TreeNode curr = queue.poll();%0A                    sum += curr.val;%0A                    if (curr.left != null) %7B%0A                        queue.offer(curr.left);%0A                    %7D%0A                    if (curr.right != null) %7B%0A                        queue.offer(curr.right);%0A                    %7D%0A                %7D%0A            %7D%0A            return sum;%0A        %7D%0A    %7D
 %0A%7D%0A

add a solution for 1302