题目描述:
计算给定二叉树的所有左叶子之和。
示例:
3
/
9 20
/
15 7
在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24
方法一:
class Solution {
public:
int sum = 0;
int sumOfLeftLeaves(TreeNode* root) {
if (root == nullptr) return 0;
if (root->left != nullptr && root->left->left == nullptr && root->left->right == nullptr)
{
sum += root->left->val;
}
sumOfLeftLeaves(root->left);
sumOfLeftLeaves(root->right);
return sum;
}
};
递归实现。



