思路:
// @Title: 左叶子之和 (Sum of Left Leaves)
// @Author: qisiii
// @Date: 2024-09-15 16:56:33
// @Runtime: 0 ms
// @Memory: 40.2 MB
// @comment:
// @flag:
/**
* 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) {
return leftSum(root);
}
private int leftSum(TreeNode root){
if(root==null){
return 0;
}
int sum=0;
if(root.left!=null&&root.left.left==null&&root.left.right==null){
sum+=root.left.val;
}
return sum+leftSum(root.left)+leftSum(root.right);
}
}