The number of nodes in the tree is between 1 and 10^4.
The value of nodes is between 1 and 100.
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;
}
}