/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int ans = 0;
while (queue.size() != 0) {
int k = queue.size();
for (int i = 0; i < k; i++) {
TreeNode node = queue.poll();
if (node.left != null) {
queue.offer(node.left);
if (node.left.left == null && node.left.right == null) ans += node.left.val;
}
if (node.right != null) {
queue.offer(node.right);
}
}
}
return ans;
}
}