Given a binary tree, return the sum of values of its deepest leaves.
Input: root = [1,2,3,4,5,null,6,7,null,null,null,null,8]
Output: 15
class Solution {
public int deepestLeavesSum(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
q.add(root);
int ans = 0;
while (q.size() != 0) {
int sum = 0, size = q.size();
while (size-- > 0) {
TreeNode node = q.poll();
if (node.left != null)
q.add(node.left);
if (node.right != null)
q.add(node.right);
sum += node.val;
}
if (q.size() == 0)
ans = sum;
}
return ans;
}
}