首页 > 其他分享 >404.sum-of-left-leaves 左叶子之和

404.sum-of-left-leaves 左叶子之和

时间:2022-09-04 10:25:41浏览次数:73  
标签:right get sum nullptr 404 root leaves left

注意判断左叶子的条件

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

相关文章