注意判断左叶子的条件
class Solution {
private:
int get_sum(TreeNode *root, int sum) {
if (root->left != nullptr && root->left->left == nullptr && root->left->right == nullptr)
sum += root->left->val;
if (root->left == nullptr && root->right == nullptr)
return sum;
if (root->left != nullptr)
sum = get_sum(root->left, sum);
if (root->right != nullptr)
sum = get_sum(root->right, sum);
return sum;
}
public:
int sumOfLeftLeaves(TreeNode *root) {
return get_sum(root, 0);
}
};
标签:right,get,sum,nullptr,404,root,leaves,left
From: https://www.cnblogs.com/zwyyy456/p/16654379.html