タイトル説明
コード
public class Solution {
public int sumOfLeftLeaves(TreeNode root){
int sum = 0;
if(root == null){
return 0;//这个判断非常重要,若缺失将引发空指针异常
}
if(root.left!=null && root.left.left==null && root.left.right == null){
sum =root.left.val;
}else{
sum += sumOfLeftLeaves(root.left);
}
sum += sumOfLeftLeaves(root.right);
return sum;
}
}
演奏