//当前节点的左子树不为空 且是叶子节点
root.left != null
&&root.left.left==null && root.left.right==null
/*** 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) {if(root == null) {return 0;}int leftSum = sumOfLeftLeaves(root.left);//当前节点的左子树不为空,且为叶子节点if(root.left != null && root.left.left==null && root.left.right==null){//加左叶子的值leftSum += root.left.val;}//右边的分支也传入,去加所在的左叶子节点int rightSum = sumOfLeftLeaves(root.right);//最后左右分支相加int ans = leftSum + rightSum;return ans;}
}```